访问JAR文件中的默认包中的类 [英] Accessing classes in default package in a JAR file

查看:378
本文介绍了访问JAR文件中的默认包中的类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Java应用程序,Java文件位于名为 app 的包中。我已经在类路径中添加了一个JAR文件。 JAR文件中的所有类都在默认包中。

I have a Java application and the Java files are in a package called app. I have added a JAR file to the classpath. All classes in the JAR file are in the default package.

从我的Java应用程序中,我无法访问此JAR文件的类。只有将我的应用程序文件放在默认包中,我才能看到JAR文件中的类。为什么?

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?

推荐答案

你不能再从默认包中 import 。 (您以前可以在Sun的早期javac实现中使用,但在语言规范中不允许这样做。)明显的解决方案:

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:


  • 如果可能,请勿使用默认包。将所有类,接口和枚举移动到命名包中。 [good]

  • 将打包的类移动到默认包中。 [bad]

  • 通过反射访问课程。 [太糟糕了]

这篇关于访问JAR文件中的默认包中的类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆