Discussion:
[protobuf] Multiple files compiled into one outer class
Niklas Keller
2016-01-03 16:18:47 UTC
Permalink
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";"?*
--
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+***@googlegroups.com.
To post to this group, send email to ***@googlegroups.com.
Visit this group at https://groups.google.com/group/protobuf.
For more options, visit https://groups.google.com/d/optout.
'Feng Xiao' via Protocol Buffers
2016-01-05 02:20:54 UTC
Permalink
Post by Niklas Keller
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
*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:*
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";"?*
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.
Post by Niklas Keller
--
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
Visit this group at https://groups.google.com/group/protobuf.
For more options, visit https://groups.google.com/d/optout.
--
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+***@googlegroups.com.
To post to this group, send email to ***@googlegroups.com.
Visit this group at https://groups.google.com/group/protobuf.
For more options, visit https://groups.google.com/d/optout.
Niklas Keller
2016-01-05 11:25:02 UTC
Permalink
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?
--
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+***@googlegroups.com.
To post to this group, send email to ***@googlegroups.com.
Visit this group at https://groups.google.com/group/protobuf.
For more options, visit https://groups.google.com/d/optout.
'Feng Xiao' via Protocol Buffers
2016-01-05 18:37:28 UTC
Permalink
Post by Niklas Keller
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 = "...";'.
Post by Niklas Keller
--
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
Visit this group at https://groups.google.com/group/protobuf.
For more options, visit https://groups.google.com/d/optout.
--
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+***@googlegroups.com.
To post to this group, send email to ***@googlegroups.com.
Visit this group at https://groups.google.com/group/protobuf.
For more options, visit https://groups.google.com/d/optout.
Continue reading on narkive:
Loading...