Multiple files compiled into one outer class

2,380 views
Skip to first unread message

Niklas Keller

unread,
Jan 4, 2016, 8:36:23 PM1/4/16
to Protocol Buffers
Morning,

I pretty new to protobuf, but I already got the basics and compiled all my definitions in a single file to a Java class called "Proto". I wanted to split the definitions into separate files, mainly to get the advantage of being able to include them into a Latex document separately for documentation purposes.

Having all definitions in separate files results one of the following problems:

1) Compiling into separate files:

Classes are in a separate "proto" package, but are always inner classes of NameInCamelCaseOuterClass.

TemplateOuterClass.Template template = TemplateOuterClass.Template.newBuilder().setId(1).setName("XY").setContent("...").build();

2) Compiling into one file:

Setting "option java_outer_classname = "Proto";" results in:

edu/.../proto/Proto.java: Tried to write the same file twice.

Is there any way having separate files but one outer class named "Proto" just like when all definitions are in one file with "option java_outer_classname = "Proto";"?

Feng Xiao

unread,
Jan 4, 2016, 9:21:00 PM1/4/16
to Niklas Keller, Protocol Buffers
That is not possible. Each proto file must be in a stand-alone Java class file. You can though, use a "java_multiple_files" option to generate the classes outside of the outer class. Try adding this to your proto file:
option java_multiple_files = true;

After this the Template class will be generated separately from the TemplateOuterClass and each message type will have its own Java class file.
 

--
You received this message because you are subscribed to the Google Groups "Protocol Buffers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to protobuf+u...@googlegroups.com.
To post to this group, send email to prot...@googlegroups.com.
Visit this group at https://groups.google.com/group/protobuf.
For more options, visit https://groups.google.com/d/optout.

Niklas Keller

unread,
Jan 5, 2016, 6:25:03 AM1/5/16
to Protocol Buffers, nicks.po...@gmail.com
That's a good solution, thanks.

However, I don't understand why the TemplateOuterClass is still needed then. Isn't everything it provides already in Template?

Feng Xiao

unread,
Jan 5, 2016, 1:37:36 PM1/5/16
to Niklas Keller, Protocol Buffers
On Tue, Jan 5, 2016 at 3:25 AM, Niklas Keller <nicks.po...@gmail.com> wrote:
That's a good solution, thanks.

However, I don't understand why the TemplateOuterClass is still needed then. Isn't everything it provides already in Template?
The outer class still contains the meta information (descriptors) of the proto file and will always be generated. You can also specify a different name for it by using 'option java_outer_classname = "...";'.
Reply all
Reply to author
Forward
0 new messages