protobuf-3.0.0-beta-1 Java, protoc can not recognize imported types

549 views
Skip to first unread message

George Wang

unread,
Dec 22, 2015, 12:18:09 AM12/22/15
to Protocol Buffers

Hello, 

I am using the released java package of protobuf-3.0.0-beta-1 (protobuf-java-3.0.0-beta-1.zip), 

I have problem of having protoc to import and recognize a type: 

I have two proto files ("gubmessage.proto","messageheaders.proto") in the source directory (see below)


the "gubmessage.proto" import the"messageheaders.proto"; and also import the "any.proto" under protobuf-3.0.0-beta-1/src/google/protobuf (see below) 
----------------------------------------
syntax = "proto3";

import "messageheaders.proto";
import "google/protobuf/any.proto";

package gubmessage;

option java_generate_equals_and_hash = true;
option java_multiple_files = true;
option java_package = "com.gubnoi.protocol";
option java_outer_classname = "GubMessage";

message GMessage{
  Headers headers = 1;  
  Any body  = 2 ;   
}
-------------------------------------------
 the messageheaders.proto 
------------------------------------------
syntax = "proto3";

package messageheaders;
option java_package = "com.gubnoi.protocol";
option java_outer_classname = "MessageHeaders";

message Headers{
 
        bool censured = 1;
        string header = 2;
repeated string tag = 3 ;
}
-------------------------------------

then I run the command " protoc --proto_path=$FR --proto_path=./google/protobuf --java_out=$TO $FR/gubmessage.proto", 

the environment FR refers to the source directory, and the TO refers to the destination directory

the protoc replied that the imported types "Headers" and "Any" are not defined. in the mean time, there are also two warnings say that the imports are not used (see below) 



any advice?

thanks 

Feng Xiao

unread,
Dec 22, 2015, 2:17:20 PM12/22/15
to George Wang, Protocol Buffers
On Mon, Dec 21, 2015 at 8:46 PM, George Wang <wangjin...@gmail.com> wrote:

Hello, 

I am using the released java package of protobuf-3.0.0-beta-1 (protobuf-java-3.0.0-beta-1.zip), 

I have problem of having protoc to import and recognize a type: 

I have two proto files ("gubmessage.proto","messageheaders.proto") in the source directory (see below)


the "gubmessage.proto" import the"messageheaders.proto"; and also import the "any.proto" under protobuf-3.0.0-beta-1/src/google/protobuf (see below) 
----------------------------------------
syntax = "proto3";

import "messageheaders.proto";
import "google/protobuf/any.proto";

package gubmessage;

option java_generate_equals_and_hash = true;
option java_multiple_files = true;
option java_package = "com.gubnoi.protocol";
option java_outer_classname = "GubMessage";

message GMessage{
  Headers headers = 1;  
  Any body  = 2 ;   
Protobuf imports and name references work much like C++. The full name of the Header message is "messageheaders.Headers". Since this file uses a different package from "messageheaders". It needs to reference the message using its fully qualified name, i.e., you need to declare it as "messageheaders.Headers headers = 1;" here. The same for Any. It should be "google.protobuf.Any body = 2;".
 
}
-------------------------------------------
 the messageheaders.proto 
------------------------------------------
syntax = "proto3";

package messageheaders;
You are giving these two protos the Java package, why not use the same package as well?
 
option java_package = "com.gubnoi.protocol";
option java_outer_classname = "MessageHeaders";

message Headers{
 
        bool censured = 1;
        string header = 2;
repeated string tag = 3 ;
}
-------------------------------------

then I run the command " protoc --proto_path=$FR --proto_path=./google/protobuf --java_out=$TO $FR/gubmessage.proto", 

the environment FR refers to the source directory, and the TO refers to the destination directory

the protoc replied that the imported types "Headers" and "Any" are not defined. in the mean time, there are also two warnings say that the imports are not used (see below) 



any advice?

thanks 

--
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.

George Wang

unread,
Dec 28, 2015, 1:34:10 PM12/28/15
to Protocol Buffers

Hello, Feng Xiao, 

I followed you advice, and got through with the protoc, thanks. 

yet there emerged new problems. 

I actually configured to export all the protocol buffer generated results into a Java package in the Eclipse (see the screenshot), so that I can applied them  as standard package repeatedly


 I notice some problems:

1: there are three items instead of one; 
according to previous experience, there should be only one file with the java_outer_classname, which is  "GubMessage" in this context; yet you can see that the inner class, (the "GMessage" in this case) is also presented under the package on the same hierarchy with "GubMessage", you can see that two java files "GMessage.java", "GMessageOrBuilder" are now presented in parallel with "GubMessage.java". is this a problem or intended result?

2, you might also notice that there were some errors existed in the java files. I checked one of them (see screenshot),  when I changed to "com.gubnoi.protocol.MessageHeaders.Header", the error disappeared.  it seems to me that protoc had not handled the outer class properly while importing types




Cheers

George Wang

unread,
Dec 28, 2015, 1:34:10 PM12/28/15
to Protocol Buffers, wangjin...@gmail.com
Hello, Feng Xiao, 

I followed you advice, and got through with the protoc, thanks. 

yet there emerged new problems. 

I actually configured to export all the protocol buffer generated results into a Java package in the Eclipse (see the screenshot), so that I can applied them  as standard package repeatedly


 I notice some problems:

1: there are three items instead of one; 
according to previous experience, there should be only one file with the java_outer_classname, which is  "GubMessage" in this context; yet you can see that the inner class, (the "GMessage" in this case) is also presented under the package on the same hierarchy with "GubMessage", you can see that two java files "GMessage.java", "GMessageOrBuilder" are now presented in parallel with "GubMessage.java". is this a problem or intended result?

2, you might also notice that there were some errors existed in the java files. I checked one of them (see screenshot),  when I changed to "com.gubnoi.protocol.MessageHeaders.Header", the error disappeared.  it seems to me that protoc had not handled the outer class properly while importing types




Cheers

George Wang

unread,
Dec 28, 2015, 1:34:11 PM12/28/15
to Protocol Buffers
Hello, Feng Xiao, 

I followed you advice, and got through with the protoc, thanks. 

yet there emerged new problems as I proceeded. 

I had configured to export all the protocol buffer generated results into a Java package in the Eclipse (see the screenshot)


 I noticed some problems here:

1: there are three items instead of one; 
according to previous experience, there should be only one file with the java_outer_classname, which is  "GubMessage" in this context; yet you can see that the inner class, (the "GMessage" in this case) is also presented under the package on the same hierarchy with "GubMessage", you can see that two java files "GMessage.java", "GMessageOrBuilder" are now presented in parallel with "GubMessage.java". is this a problem or intended result?

2, you might also notice that there were some errors existed in the java files. I checked one of them (see screenshot),  when I changed to "com.gubnoi.protocol.MessageHeaders.Header", the error disappeared.  it seems to me that protoc had not handled the outer class properly while importing types




Cheers


On Tuesday, December 22, 2015 at 1:18:09 PM UTC+8, George Wang wrote:

Feng Xiao

unread,
Dec 28, 2015, 1:40:07 PM12/28/15
to George Wang, Protocol Buffers
On Tue, Dec 22, 2015 at 7:09 PM, George Wang <wangjin...@gmail.com> wrote:
Hello, Feng Xiao, 

I followed you advice, and got through with the protoc, thanks. 

yet there emerged new problems. 

I actually configured to export all the protocol buffer generated results into a Java package in the Eclipse (see the screenshot), so that I can applied them  as standard package repeatedly


 I notice some problems:

1: there are three items instead of one; 
according to previous experience, there should be only one file with the java_outer_classname, which is  "GubMessage" in this context; yet you can see that the inner class, (the "GMessage" in this case) is also presented under the package on the same hierarchy with "GubMessage", you can see that two java files "GMessage.java", "GMessageOrBuilder" are now presented in parallel with "GubMessage.java". is this a problem or intended result?

That's because you have specified the "java_multiple_files = true" option in the .proto file. The error you see may also be related to this. Seems like you have changed these options between invoking protoc on each .proto file. You need to regenerate all your files against the same version of the .proto files.
Reply all
Reply to author
Forward
0 new messages