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.
Related
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.
So, I'm trying to get into LWJGL, and part of this is loading 3D models, and I thought I'd use JAssimp for this. The readme specifies two steps, building a native component, and a .jar component. The jar was just an ant build, and so it happened without incident. The part that's difficult is the native library. I've been trying to link it, without success. I've included the assimp include directories, included Java's headers, and linked to JAssimp's assimp.lib. However, I still get these errors:
jassimp.obj : error LNK2019: unresolved external symbol _aiImportFile referenced in function _Java_jassimp_Jassimp_aiImportFile#20
jassimp.obj : error LNK2019: unresolved external symbol _aiReleaseImport referenced in function _Java_jassimp_Jassimp_aiImportFile#20
jassimp.obj : error LNK2019: unresolved external symbol _aiGetErrorString referenced in function _Java_jassimp_Jassimp_aiImportFile#20
C:\Users\Kevin\Downloads\assimp-3.1.1-win-binaries\port\jassimp\jassimp-native\src\Release\Jassimp.dll : fatal error LNK1120: 3 unresolved externals
Googling around leads to mostly the same question, but with no answers, and one guy on facepunch that apparently found a solution, but pulled an https://xkcd.com/979/ and never mentioned how. Obviously, I've missed linking to some library, but I'm not sure what else to link to, Assimp only provided me one, and I'm not sure what else I need. Am I installing it wrong?
For anyone else who stumbles across this with the same problem, the answer is to do it the old-fashioned way and compile the C++ Assimp yourself. This will give you, (among other things) a new assimp.lib. Link with this instead of the one provided, and the error will go away.
How To Build
I) buiid the primary shared object libassimp.so: issue these commands in AssImp/assimp-3.2
a. cmake CMakeLists.txt -G 'Unix Makefiles'
b. make
c. results in AssImp/assimp-3.2/lib
II) build the native library by issuing this command in jassimp-native/src:
a. $ gcc jassimp.cpp -I/usr/lib/jvm/java-7-openjdk-amd64/include/ \
-I/usr/lib/jvm/java-7-openjdk-amd64/include/linux \
-I/$HOME/AssImp/assimp-3.2/include \
-L/$HOME/AssImp/assimp-3.2/lib/libassimp.so -shared -fPIC -o libjassimp.so
libjassimp.so is required at runtime and must be located in java.library.path:
b. sudo cp /$HOME/AssImp/assimp-3.2/port/jassimp/jassimp-native/src/libjassimp.so /usr/lib/jvm/java-7-openjdk-amd64/jre/lib/amd64/
III) Java binding
The java library is built using ant. Executing "ant" in the port/jassimp
directory should be sufficient to build the library including the docs.
The java build is configured to create java 1.6 classes
The Jassimp.jar file generated by the "ant" call has to be incorporated
in your build.xml file:
<target name="init">
<property name="imports" value="${libs}/jogl-all.jar:${libs}/gluegen-rt.jar:${libs}/jassimp.jar" />
<target name="compile" depends="prepare">
<compilerarg line="-Xlint -cp ${imports}" />
All of the above is the good news; I successfully built my code.
The bad news is that upon execution I received the following error:
java: symbol lookup error: /usr/lib/jvm/java-7-openjdk-amd64/jre/lib/amd64/libjassimp.so: undefined symbol: aiImportFile
I'm new to protobufs and was trying to learn more about using them. I've downloaded the protobuf packaged from here. There is a README.txt file inside the examples folder of the archive which gives instructions on how to build 2 example applications. However when I follow those instructions for building the java application:
make java
I get a lot of errors followed by:
100 errors
make: * [javac_middleman] Error 1
All of the 100 errors seem to be classpath related, as this is a typical example:
com/example/tutorial/AddressBookProtos.java:37: error: package com.google.protobuf does not exist
Any ideas about how to get passed this?
The problem is that for some reason protobuf jar is not added to the classpath during compilation. To fix it you should open examples/Makefile and add -cp protobuf-java-2.4.1.jar at the end of java complilation line javac AddPerson.java ListPeople.java com/example/tutorial/AddressBookProtos.java.
P.S. If you built you protobufs with maven the jar is located at ~/.m2/repository/com/google/protobuf/protobuf-java/2.4.1/protobuf-java-2.4.1.jar (version of the jar might be different)
I have just written a Java multi-threaded program in Eclipse. It compiled fine and works like a charm.
However, as this is coursework we are required to ensure that it compiles in the command line using 'javac' otherwise we score ZERO!
So, some classes compile others don't. The error I'm getting is the following ( they are all similar just with different class names, this is one example)
GateRunnable.java:7: cannot find symbol
symbol : class Station
location: class package.name.here.GateRunnable
public GateRunnable(Station st) {
^
Is this a javac issue? Any help appreciated.
Your compile -classpath and/or -sourcepath is incomplete. The compiler doesn't know where to find class Station. Here is a related question that describes how to set the classpath to include all the classes you want.
To solve the problem I was having, it was simply necessary to compile all classes by using the following command:
javac *.java
which compiles all java files in the directory.
have you compiled every .java file in your folder/packages? if not, then do so. Eclipse usually does that for you, but within the terminal it's you taking the responsibility of compiling every part of the code.
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