Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Importing "google/protobuf/descriptor.proto" in java protocol buffers

I have a .proto file definition which needs to import "google/protobuf/descriptor.proto" because I use Custom Options.

So in my .proto file I do:

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

...

Since my file didn't compile complaining about the dependency, I got a copy of the descriptor.proto file placing it in the same directory my proto file was.

This solved the problem but I don't believe this is the correct way. Now the descriptor.proto gets compiled together with my .proto file resulting in having 2 compiled descriptor.proto at runtime:

  • the one shipped with the protobuf-java-2.5.0.jar file
  • the one which was compiled together with my .proto file

I think the --proto-path option should be used somehow but not entirely sure what is the correct way.

Thanks for the best practise tip here!

like image 734
Jan Zyka Avatar asked Nov 19 '13 10:11

Jan Zyka


People also ask

Can you mix proto2 and proto3?

It's possible to import proto3 message types and use them in your proto2 messages, and vice versa. However, proto2 enums cannot be used in proto3 syntax.

What is Protobuf descriptor?

descriptor. Descriptors essentially contain exactly the information found in a . proto file, in types that make this information accessible in Python.

What are Protobuf Protocol Buffers useful for?

Protocol Buffers (Protobuf) is a free and open-source cross-platform data format used to serialize structured data. It is useful in developing programs to communicate with each other over a network or for storing data.

Is proto3 backwards compatible with proto2?

Yes, if some of your systems are proto2 based, it is probably best to keep using proto2. In my opinion, proto3 does not introduce many new features and most libraries will continue supporting proto2. However, the wire format is mostly compatible. As long as the tag number is the same, the encoding remains the same.


2 Answers

When I have used descriptor in a .proto, I have used it like

import "google/protobuf/descriptor.proto";

message AddressBook {
  required google.protobuf.FileDescriptorSet proto_files = 1;

Then to generate the java (on windows) with 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 protocol buffers install directory. The key point is descriptor.proto is in

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

The levels in an protobuf import stament must match directories in the File system just like they would in java.

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

<protobuf-install-directory>\src
    +-- google
         +-- protobuf
             +-- descriptor.proto
like image 61
Bruce Martin Avatar answered Nov 15 '22 17:11

Bruce Martin


Your protoc is not able to find the files in the default include folder for your system

https://github.com/golang/protobuf/issues/694

apt install protobuf-compiler does not put it in include folder

Use this if you are having errors like in linux machines

google/protobuf/descriptor.proto: File not found.

google/protobuf/duration.proto: File not found.

google/protobuf/timestamp.proto: File not found.

For Correct installation on linux systems

PROTOC_ZIP=protoc-3.7.1-linux-x86_64.zip
curl -OL https://github.com/protocolbuffers/protobuf/releases/download/v3.7.1/$PROTOC_ZIP
sudo unzip -o $PROTOC_ZIP -d /usr/local bin/protoc
sudo unzip -o $PROTOC_ZIP -d /usr/local 'include/*'
rm -f $PROTOC_ZIP
like image 34
Jital Patel Avatar answered Nov 15 '22 17:11

Jital Patel