9

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
1
  • 1
    The README really couldn't have made it any clearer: You will still need to download the source code package in order to obtain the Java or Python runtime libraries. Get it from: github.com/google/protobuf/releases Commented Aug 9, 2015 at 23:10

3 Answers 3

36

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.

Sign up to request clarification or add additional context in comments.

3 Comments

@Timmmm: This helped.. thanks.. But anyway i just couldn't understand the meaning of the command "protoc --java_out=src/main/java -I../src ../src/google/protobuf/descriptor.proto" though i ve got a vague idea.. So can u please clearly explain??
@aarish run protoc -h. -I is the directory to search for imports, --java_out is the directory in which to place the generated source files and the last argument is the proto file to compile
from README.md directory, open README.md and execute command like protoc --java_out=core/src/main/java -I../src \ ../src/google/protobuf/descriptor.proto if you are new comer of protobuf as I do with version 3.3.0. The directory structure maybe be changed as version changes. @Aarish
0

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 :)

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.