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:
protobuf-java-2.5.0.jar
file.proto
fileI 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!
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.
descriptor. Descriptors essentially contain exactly the information found in a . proto file, in types that make this information accessible in Python.
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.
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.
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With