Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Protoc: How to generate multiple Java source files?

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?

like image 792
Robert Avatar asked Oct 02 '14 14:10

Robert


People also ask

Is there a way to let protoc generate multiple source code files?

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.

What does protoc do in Java?

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.

How to generate classes from a proto file in Java?

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>

How does Protobuf generate Java source files?

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.


1 Answers

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.

like image 138
Kenton Varda Avatar answered Oct 03 '22 19:10

Kenton Varda