Timmmm
Timmmm

Reputation: 97118

Protobuf java code has build errors

If you get build errors like these when using protobufs with java, look 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

Upvotes: 9

Views: 13984

Answers (3)

anql
anql

Reputation: 39

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

Installation - Without Maven

If you would rather not install Maven to build the library, you may follow these instructions instead. Note that these instructions skip running unit tests and only describes how to install the core protobuf library (without the util package).

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

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

2) Invoke protoc to build DescriptorProtos.java:

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

4) Install the classes wherever you prefer.

Upvotes: 0

doc
doc

Reputation: 817

Another option is to edit the pom.xml included in the source. You can change it to compile the proto files at the validate life-cycle and write them into 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 just run mvn validate and all the proto files will be compiled into the source of your project :)

Upvotes: 0

Timmmm
Timmmm

Reputation: 97118

Ok, the so-called Java tutorial for protobufs doesn't actually mention how to get the protobuf library into your project. It implies that all the code is in your single generated .java file, which would actually be pretty nice, but that isn't case.

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

The solution is in the README.txt file. Yeah, maybe I should have read it, but shouldn't all the information you need to get started be in the getting started tutorial? Anyway, do this:

# 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 into your project.

Upvotes: 36

Related Questions