There are build errors in Protobuf java code

If you get build errors like these when using protobufs with java, see below.

The method getOptions() from the type Descriptors.Descriptor refers to the missing type MessageOptions The import com.google.protobuf.DescriptorProtos cannot be resolved FileDescriptorProto cannot be resolved to a type 
+7
source share
3 answers

Well, the so-called protobufs Java tutorial doesn't actually mention how to get the protobuf library in your project. This implies that all the code is in your single .java file, which would actually be pretty nice, but this is not the case.

Look at the source and you will see links to com.google.protobuf , which you can find in the java/src/main/java directory in the protobuf source. Copy this into your project and it will have build errors.

The solution is in the README.txt . Yes, maybe I should read this, but shouldn't all the information you need to get started begin with a textbook? In any case, do the following:

 # From the protobuf directory. cd java protoc --java_out=src/main/java -I../src ../src/google/protobuf/descriptor.proto 

And then copy the java files to your project.

+31
source

Another option is to edit the pom.xml file included in the source. You can modify it to compile proto files during the life cycle check and write them to the source directory.

Apply this diff or similar (or create a new build profile):

 $ diff -u ~/Downloads/protobuf-2.6.0/java/pom.xml pom.xml --- /c/Users/MYNAME/Downloads/protobuf-2.6.0/java/pom.xml Mon Aug 25 20:52:36 2014 +++ pom.xml Tue Dec 2 13:51:56 2014 @@ -74,12 +74,12 @@ <executions> <execution> <id>generate-sources</id> - <phase>generate-sources</phase> + <phase>validate</phase> <configuration> <tasks> <mkdir dir="target/generated-sources" /> - <exec executable="../src/protoc"> - <arg value="--java_out=target/generated-sources" /> + <exec executable="protoc"> + <arg value="--java_out=src/main/java" /> <arg value="--proto_path=../src" /> <arg value="../src/google/protobuf/descriptor.proto" /> </exec> @@ -92,12 +92,12 @@ </execution> <execution> <id>generate-test-sources</id> - <phase>generate-test-sources</phase> + <phase>validate</phase> <configuration> <tasks> <mkdir dir="target/generated-test-sources" /> - <exec executable="../src/protoc"> - <arg value="--java_out=target/generated-test-sources" /> + <exec executable="protoc"> + <arg value="--java_out=src/test/java" /> <arg value="--proto_path=../src" /> <arg value="--proto_path=src/test/java" /> <arg value="../src/google/protobuf/unittest.proto" /> 

Now you can simply run mvn validate , and all proto files will be compiled into the source code of your project :)

0
source

https://github.com/google/protobuf/tree/master/java

Installation - without Maven

If you do not want to install Maven to create the library, you can follow these instructions. Please note that these instructions skipped the execution of unit tests and described only how to install the main protobuf library (without the util package).

1) Create C ++ code or get the binary distribution of protoc. If you are installing a binary distribution, make sure that it is the same version as this package. If in doubt, run:

$ protoc -version If you built C ++ code without installation, the compiler binary should be located in .. / src.

2) Call protoc to create the Protos.java descriptor:

$ protoc --java_out = core / src / main / java -I ../ src \ .. /src/google/protobuf/descriptor.proto 3) Compile the code in the kernel / src / main / java using any means that you prefer.

4) Install classes wherever you wish.

0
source

All Articles