开发者

Accessing classes in default package in a JAR file

I have a Java application and the Java files are in a package called app. I h开发者_运维百科ave added a JAR file to the classpath. All classes in the JAR file are in the default package.

From my Java application I can't access classes of this JAR file. Only if I put my application files in the default package, then I can see the classes in the JAR file. Why is that?


You can no longer import from default packages. (You used to be able to in Sun's early javac implementations, but this was not allowed for in the language spec.) Obvious solutions:

  • Don't use the default package, if possible. Move all classes, interfaces and enums into named packages. [good]
  • Move your packaged classes into the default package. [bad]
  • Access the classes via reflection. [terrible]


Classes in the default package are not visible to classes inside a package.

When classes are in a different package, then you basically need to import classes. But since it's impossible to import classes in the default package, the only solution is to put those classes in a package.

Always put classes in a package whenever you want to reuse classes.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜