3

I'm working with Google's Protocol Buffer (in combination with the Protocol Buffers maven plugin) which compiles a .proto file into a class. I can use the generated class in the default package perfectly, but not outside of it. I don't really know how to explain it any better so I'm going to show you some pictures.

Working Not working

I've tried subclassing the Hrp class but that doesn't work (the generated class is final). It is also not an option to move the class every time I re-generate the Hrp class.

I'm not sure if this is relevant, but the generated class is public final. It contains an empty, private constructor.

I have also tried setting the generated sources package prefix for the generated sources folder but that also does not work.

Any help would be greatly appreciated.

nbokmans
  • 5,492
  • 4
  • 35
  • 59
  • 1
    Try specifying a package id in the protocol buffers definition (https://developers.google.com/protocol-buffers/docs/reference/java-generated#package) and then importing from the package id Java. You can import the default package id in Java – Bruce Martin Apr 13 '16 at 21:05
  • That's what I ended up doing and it seems to work now. Thank you! – nbokmans Apr 18 '16 at 10:57
  • I will add it as an answer – Bruce Martin Apr 18 '16 at 11:21

1 Answers1

2

Try adding a package id to your Protocol Buffers definition. See Protocol Buffers Package

i.e.

syntax = "proto3";
package MyPackage;
option optimize_for = SPEED;

message Product {
   repeated ASale sale = 1; 
}

Then when you Generate the Java~Protocol~Buffers code (using protoc), it will be in package MyPackage and you will be able to import it into your java code in the normal way.

In java, you can not import anything from the Default package; which I believe is your problem. See How to access java-classes in the default-package?

Community
  • 1
  • 1
Bruce Martin
  • 10,358
  • 1
  • 27
  • 38