Java packages, problem with default package for Nanopb

513 views
Skip to first unread message

trepid...@gmail.com

unread,
Feb 4, 2013, 11:38:07 AM2/4/13
to nan...@googlegroups.com
Just been trying out nanopb, seems great so far. One minor thing I've noticed when setting things up for communication from C to Java, is that when I generate Java source, it creates a class from the nanopb.proto file, in the default package, which are referenced from my own proto classes. This causes a problem, since I can't import the default package. There seems to be a quick solution for this, by adding the following option to nanopb.proto:

option java_package = "fi.kapsi.koti.jpa.nanopb";

Or the same with your choice of non-empty package! This then generates Java code that works fine.
I don't think this has any bad side-effects, would it be worth including this in Nanopb by default?

petteri...@gmail.com

unread,
Feb 4, 2013, 12:03:42 PM2/4/13
to nan...@googlegroups.com
Hi,

> it creates a class from the nanopb.proto file, in the default package,
> which are referenced from my own proto classes. This causes a problem,
> since I can't import the default package. There seems to be a quick
> solution for this, by adding the following option to nanopb.proto:
>
> option java_package = "fi.kapsi.koti.jpa.nanopb";

This seems reasonable.

However, I would like to understand the problem better (I'm not a Java
expert myself): what do you mean by "can't import the default package"?

--
Petteri

trepid...@gmail.com

unread,
Feb 4, 2013, 3:39:34 PM2/4/13
to nan...@googlegroups.com
> However, I would like to understand the problem better (I'm not a Java
>
> expert myself): what do you mean by "can't import the default package"?

I'll have a go at explaining, it's actually the first time I've run into this kind of thing; it's a bit unusual to have code in the default package, so I also had to look it up to work out what is going on!

Normally any Java class will be in a named package, often a "backwards domain name" like "org.someproject.somepackage". It's also possible to have no package declaration at all, which gives you the "default" package, essentially an empty package name.

However this is deprecated and strongly discouraged, since it tends to pollute the namespace. In fact it is now apparently not possible for classes in a named package to import classes from the default package:
http://stackoverflow.com/questions/2030148/whats-the-syntax-to-import-a-class-in-a-default-package-in-java

When protobuf generates the java class for a .proto with no package or java_package specified, it puts the output java class in the default package. This happens to the nanopb.proto, generating a class Nanopb.java with no package declaration. If you also have a "foo.proto" file with package or java_package set to "bar" then you will also get a foo.java file in the bar package. This foo.java file will try to reference Nanopb.java for the extension stuff. Because of the restriction on importing from the default package, this generates a compile error. If the Nanopb.java file lives in a named package, everything seems to work fine.

Things should also work if none of the .proto files specifies a package or java_package, since everything will then be in the default package, and apparently this will work. However it's also far from ideal, since the default package is deprecated.

petteri...@gmail.com

unread,
Feb 5, 2013, 4:51:38 AM2/5/13
to nan...@googlegroups.com
Hi,

Thanks for the explanation. Your fix has been committed:

https://code.google.com/p/nanopb/source/detail?r=38ced18639f1e2236e9949071e3ddc148cf2ad73

--
Petteri
Reply all
Reply to author
Forward
0 new messages