1

I have downloaded protocol buffers (2.6.0) and I am trying to compile the .proto files. I run the following from inside the downloaded protobuf-2.6.0 directory

$ protoc --version
libprotoc-2.6.0

$ cd java
$ protoc --java_out=src/main/java -I../src ../src/google/protobuf/descriptor.proto

$ ls src/main/java
com

There are no errors on the protoc command, but the compilation should have generated source files in src/main/java/google/protobuf

  • --java_out specifies where generated Java source files should go
  • -I specifies the directory where imports can be found

I see nothing wrong with my command. Any ideas?

Thanks

1
  • Ah! This question is not asking the right thing. The source files are generated, but the package differs to what I expected. The descriptor.proto file has 'package google.protobuf', but the sources are put in 'com.google.protubuf'. Should I delete the question, or edit it? Commented Dec 2, 2014 at 13:39

2 Answers 2

1

Your -I option seems incomplete. It should be

-I../src/google/protobuf/
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks - unfortunately, adding the full path to the import statement did not change the result (I assumed it searched -I recursively).
This answer is incorrect. The question's original -I../src is correct. -I should specify the "canonical source root". Since the file is always imported as import "google/protobuf/descriptor.proto;, the -I flag should NOT include google/protobuf. (In this case, the difference is subtle. Specifying -I../src/google/protobuf will not cause an error, but the source code's embedded descriptor will be wrong.)
1

The question premise is false. The sources are being generated, but go under src/main/java/com/google/protobuf rather than src/main/java/google/protobuf as one might expect from the package name in descriptor.proto.

Edit - the java package name is given in the proto file like so:

package google.protobuf;
option java_package = "com.google.protobuf";

1 Comment

Correct. Java code is expected to be organized by Java package name. In contrast, C++ generated code will be organized according to the directory structure of the input .proto files.

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.