Import "google / protobuf / descriptor.proto" in Java protocol buffers

I have a .proto file .proto that I need to import "google/protobuf/descriptor.proto" because I am using Custom Settings .

So in my .proto file I do:

 import "google/protobuf/descriptor.proto"; package ...; ... 

Since my file did not compile dependency complaints, I received a copy of descriptor.proto, placing it in the same directory as my proto file.

This solved the problem, but I do not believe that this is the right way. Now descriptor.proto .proto with my .proto file, resulting in 2 compiled descriptor.proto compiled at runtime:

  • one that comes with protobuf-java-2.5.0.jar
  • the one that was compiled with my .proto file

I think the --proto-path option should be used somehow, but not quite sure if this is the correct way.

Thanks for the best advice here!

+5
source share
1 answer

When I used the handle in .proto, I used it as

 import "google/protobuf/descriptor.proto"; message AddressBook { required google.protobuf.FileDescriptorSet proto_files = 1; 

Then, to generate java (in windows) using addressbookSD.proto in the default directory:

 protoc addressbookSD.proto --java_out=./ --proto_path=./ --proto_path=<protobuf-install-directory>\src 

where < protobuf-install-directory > is the directory for installing the protocol buffers. The key point is the .proto handle is in

 <protobuf-install-directory>\src\google\protobuf 

The levels in protobuf import stament should match the directories in the File System, as in java.

So I use < protobuf-install-directory> \ src as the import directory, the directory structure should be

 <protobuf-install-directory>\src +-- google +-- protobuf +-- descriptor.proto 
+5
source

Source: https://habr.com/ru/post/958383/


All Articles