After compiling a large Protobuf definition I am getting a 6 MB Java source code file.
Because of its size working with that file is a big pain as I develop in Eclipse and Eclipse totally stalls/crashes whenever I open that file.
Is there a way to let protoc generate multiple Java source code files instead of one big file?
Is there a way to let protoc generate multiple Java source code files instead of one big file? Show activity on this post. Actually, there is. It's not documented, but you can add a line to your .proto file like this: This will put each top-level message type from the .proto file into an independent .java file.
It assembles the Protobuf Compiler (protoc) command line and uses it to generate Java source files out of your proto files. It adds the generated Java source files and document file to the input of the corresponding Java compilation unit (sourceSet in a Java project), so that they can be compiled along with your Java sources.
Start command prompt from bin folder of the downloaded application. Use the potoc command to generate the classes. protoc --proto_path=<Directory name where your proto file is residing> --java_out=<Directory name where you want your output classes to get generated> <absolute path of your protofile with extention>
It’s a two-step process: It assembles the Protobuf Compiler (protoc) command line and uses it to generate Java source files out of your proto files.
Actually, there is. It's not documented, but you can add a line to your .proto
file like this:
option java_multiple_files = true;
This will put each top-level message type from the .proto
file into an independent .java
file. Note that you'll of course have to update all your code to import these classes from their new locations. Note also that protoc
will still generate the "outer" class it did before, just without the inner classes nested within it. The outer class is still the place to go to get the file's descriptor, top-level extensions, and other non-class things.
All that said, if you have a .proto
file that big, I strongly recommend splitting up the .proto
file itself into smaller files that import each other.
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