I have two .proto
files (a.proto
and b.proto
) having the same contents in them:
syntax = "proto3";
message env {
string broker = 1;
}
When I execute the compiler (I want to generate Java source files) and specify both files on the command line
protoc.exe --java_out=. a.proto b.proto
I get error messages:
b.proto:4:10: "env.broker" is already defined in file "a.proto".
b.proto:3:9: "env" is already defined in file "a.proto".
I'd expect the compiler to generate two Java classes (A
and B
), each having a nested class Env
. This is how I understand the docs. But this does not happen.
What am I doing wrong?
Thank you for any hints.
b.proto:4:10: "env.broker" is already defined in file "a.proto".
b.proto:3:9: "env" is already defined in file "a.proto".
Most protobuf libraries put all messages with same package name into same namespace. The Java library is special in generating the outer class, which is needed because Java does not allow multiple top-level classes per file.
Protoc is doing you a service checking the rule here. If you went ahead and used the same message name in multiple files without specifying a package, you would make it very difficult for other people to use your .proto
files with different programming languages.