I am using the Play framework (which uses SBT build tool) with Java where I need to consume a Protobuf. So I have xxx.proto file. I got binary protoc compiler and added to class path. so I see -
protoc --version
libprotoc 3.1.0
I have compiled the xxx.proto file using -
protoc -I=$SRC_DIR --java_out=$DST_DIR $SRC_DIR/xxx.proto so it has generated xxx.java file.
Now when I am compiling this xxx.java file ( the project using sbt build tool)
[error] /my_project/app/helpers/xxx.java:7: package com.google.protobuf does not exist
[error] com.google.protobuf.ExtensionRegistryLite
[error] /my_project/app/helpers/xxx.java:11: package com.google.protobuf does not exist
[error] com.google.protobuf.ExtensionRegistry
[error] /my_project/app/helpers/xxx.java:6182: package com.google.protobuf.Descriptors does not exist
[error] com.google.protobuf.Descriptors.Descriptor
[error] /my_project/app/helpers/xxx.java:6185: package com.google.protobuf.GeneratedMessageV3 does not exist
[error] com.google.protobuf.GeneratedMessageV3.FieldAccessorTable`
I see in my installed library - com.google.protobuf jar is there.
My xxx.proto looks following -
// Generated by the protocol buffer compiler. DO NOT EDIT!
// source: xxx.proto
public final class xxx {
private xxx() {}
public static void registerAllExtensions(
com.google.protobuf.ExtensionRegistryLite registry) {
}
public static void registerAllExtensions(
com.google.protobuf.ExtensionRegistry registry) {
registerAllExtensions(
(com.google.protobuf.ExtensionRegistryLite) registry);
}
......
Is there anything I have missed while generating the xxx.java file?
How should I fix these compilation error?
You need to make sure that you're using the exact same versions of protoc and libprotobuf.jar. From what you wrote, it sounds like you're using protoc version 3.1.0 but libprotobuf 2.5.0. You need to use libprotobuf 3.1.0 instead, otherwise you will get compile errors like the ones you quote.
Re-stating Kenton's answer with some more instructions:
In Intellij, click on External Libraries and find the jar for protobuf.
Check the version of protoc:
If they don't match, (as shown above) then you will get the compilation errors.
I've seen similar issue with maven after changing some field type in my proto schema and then building without doing a clean first. However, doing a clean and build fixed it every time.
Related
I am using maven with the scala-maven-plugin for building a project that is a mixture of Java 1.8 and Scala 2.12.7 code. When I build from the command line with:
mvn clean install -DskipTests
I get messages like:
"[WARNING] Could not determine source for class
com.mycompany.MyClass_".
com.mycompany.MyClass is an annotated Java class that exists in my project. Note that the warning message has an underscore after "MyClass". There is no Java source code for a class named com.mycompany.MyClass_ in my project, but there is a class file at target/classes/com/mycompany/MyClass_.class.
What can I do to determine the cause of these warnings and remove them? (I can get rid of them by commenting out the scala-maven-plugin section of my pom file, but that seems a little extreme.)
Additional diagnostic data:
Following #Joachim Sauer's advice, I used javap to look at the class file. The output included:
Compiled from "MyClass_.java"
I did a search for "MyClass_.java" from the root of the project. It was found in
target/scoverage-classes/com/mycompany/MyClass_.java
When I look at that class in the editor, it includes:
#Generated(value = "org.hibernate.jpamodelgen.JPAMetaModelEntityProcessor")
#StaticMetamodel(FileMetadata.class)
So it looks like the file generated by SCoverage and/or JPAMetaModelEntityProcessor is not locatable by some other tool. I'll dig some more...
I am trying to generate a java API using swagger-codegen (3.0.0)
I am using the swagger-codegen-cli-3.0.24.jar version of codegen jar.
I ran the following command to generate the SDK for the sample petstore project:
java -jar swagger-codegen-cli.jar generate -i code/swagger-codegen/modules/swagger-codegen/src/test/resources/3_0_0/petstore.json -l java -o out/pet-java
When I am compiling the generated sdk project:
cd out/pet-java
mvn clean package
I am getting the below error:
Compilation failure
[ERROR] out/pet-java/src/main/java/io/swagger/client/model/Pet.java:[87,34] cannot find symbol
[ERROR] symbol: method nextObject()
[ERROR] location: variable jsonReader of type com.google.gson.stream.JsonReader
it is using <gson-version>2.8.1</gson-version> as dependency.
Ultimately the real answer is for swagger-codegen to fix their regression. They've admitted that it's a problem. In the mean time I've worked around the issue for my particular situation. To demonstrate the types of changes that are necessary, see below:
In some files instead of nextObject(), there may be a call to next(). The solution is the same, however.
Apologies for any confusion in the diff. Apparently I left the original lines in the file commented out below the working replacement line.
I have a Intellij plugin project that I can successfully build without using the PsiMethod class. I've generated this project using this intellij plugin guide
As soon as I add in code using PsiMethod class
(from the Intellij SDK code_samples directory)
The intellisense cannot find any GOTO information for the class.
I can then switch my project settings module from JAVA_1.8.0 to
by adding it into the platform settings SDKs:
This now adds intellisense, and gives me the ability to navigate to the PsiMethod class by CMD+click.
However, If I try and run my "plugin" gradle configratuion. it halts with an error running :runIde.
Running compileJava command from bash I get:
$ ./gradlew compileJava
> Task :compileJava FAILED
/Users/cody/IdeaProjects/plugintest/src/main/java/com/plugintest/TestClass.java:104: error: cannot find symbol
PsiMethod containingMethod = PsiTreeUtil.getParentOfType(element, PsiMethod.class);
^
symbol: class PsiMethod
location: class ParticleContainerManager
/Users/cody/IdeaProjects/plugintest/src/main/java/com/plugintest/TestClass.java:104: error: cannot find symbol
PsiMethod containingMethod = PsiTreeUtil.getParentOfType(element, PsiMethod.class);
^
symbol: class PsiMethod
location: class ParticleContainerManager
Note: Some input files use or override a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
2 errors
The gradle source sets in intellij are as follows:
And when I CMD+Click the PsiMethod class, it shows the following JAR source:
This source is in the following parent folder:
How can I add this source into my gradle configuration? shouldn't it be autodetecting what my projects intellisense is tapping into?
Do I need to change something in the gradle.* files inside the project directory?
EDIT:
Using Yann's Answer:
I've added the following line to my build.gradle file:
intellij {
version '2019.2'
plugins 'java'
}
and it works!
Please make sure to add Java plugin as separate dependency. This is now necessary starting with 2019.2: https://blog.jetbrains.com/platform/2019/06/java-functionality-extracted-as-a-plugin/
I am trying to run a project generated using jhipster. I opted to use Spring Websockets while generating the project. However on running the mvn command on the folder. I get the following error:
[ERROR] diagnostic: C:\java_dev\workspaces\pmilive\pmi\trunk\src\main\java\id\co\allianz\pension\config\WebsocketSecurityConfiguration.java:10: error:
cannot access org.springframework.web.socket.config.annotation.AbstractWebSocketMessageBrokerConfigurer
public class WebsocketSecurityConfiguration extends AbstractSecurityWebSocketMessageBrokerConfigurer {
^
class file for org.springframework.web.socket.config.annotation.AbstractWebSocketMessageBrokerConfigurer not found
[ERROR] error on execute: error during compilation
Is there any other additional setting that I should add?
No nothing special is required, I suspect that you had an error during maven dependency resolution phase. maybe something is wrong in your ~/.m2 local repository: try deleting ~/.m2/respository/org/springframework and re-run mvn.
If you're on Windows, path is %USERPROFILE%\.m2\repository\org\springframework.
I have a file xxx.proto. I downloaded the protobuf compiler and installed it. Then I issued this command
protoc --java_out=./ xxx.proto
and it generated my xxx.java
Now I want to compile this file into a class file which I can use with Scala.
javac xxx.java
Which gives me this error
xxx.java:7: package com.google.protobuf does not exist
com.google.protobuf.ExtensionRegistry registry) {
^
xxx.java:12450: package com.google.protobuf.Descriptors does not exist
private static com.google.protobuf.Descriptors.Descriptor
^
xxx.java:12453: package com.google.protobuf.GeneratedMessage does not exist
com.google.protobuf.GeneratedMessage.FieldAccessorTable
...
...
...
100 errors
Now I guessed, it doesnt have the package.
So I copied the class files of package com.google.protobuf into the same folder where xxx.java exists. Note - I didnt compile this package. I downloaded the jar from another extension which had the jar files. So I extracted them. Now my current path where xxx.java resides has com/google/protobuf/ *.class of protobuf library.
I issued the javac command again.
This time I got a different set of errors -
xxx.java:10: cannot find symbol
symbol : class MessageOrBuilder
location: package com.google.protobuf
extends com.google.protobuf.MessageOrBuilder {
^
xxx.java:215: cannot find symbol
symbol : class MessageOrBuilder
location: package com.google.protobuf
extends com.google.protobuf.MessageOrBuilder {
^
xxx.java:608: cannot find symbol
symbol : class MessageOrBuilder
location: package com.google.protobuf
extends com.google.protobuf.MessageOrBuilder {
^
xxx.java:1017: cannot find symbol
symbol : class MessageOrBuilder
location: package com.google.protobuf
extends com.google.protobuf.MessageOrBuilder {
..... 100 errors
I even tried to compile the source files which came with google protobufs. The generated java classes are giving the same errors.
Any ideas what to do ??
Answer
Okay. Thanks everyone.
The main problem is that protocol buffers compiler package from google doesnt by default create the java library. I assumed that it does and installs it. It actually does if you are running Maven. But i didnt have maven
So i compiled the code in /java/src and used the jar.
^
When compiling, you need to have protobuf lib on your classpath. All those missing packages and classes are from protobuf lib.
Find protobuf jar and use
javac -cp path/to/protobuf.jar xxx.java
You may need to use version 2.4.1 (or 2.4+, at least) of the protobuf kit, including making sure that you update protoc (the protobuf compiler) and recompile your proto definition using the new protoc. (In other words, everything has to be the same version:
the protobuf-vn.n.n.jar file;
the protoc compiler; and
the output of compiling your .proto files with protoc.
One I got everything synched, I began to move forward with a Clojure project I'm looking at. You may be encountering the same version skew problem.
protoc -I=$SRC_DIR --java_out=$DST_DIR $SRC_DIR/addressbook.proto
you can download protoc.exe (new release) from>>.
https://code.google.com/p/protobuf/downloads/detail?name=protoc-2.5.0-win32.zip&can=2&q=
in your *.proto file you correctly config
option java_package = "com.example.package";
option java_outer_classname = "class name";
One can install the protobuf jar file using the ubuntu a
apt-get install libprotobuf-java
This will copy the protobuf-java-2.4.1.jar under /usr/share/java/
Hope this helps