程序从 jar 执行时的动态加载问题
我有一个可通过符合给定接口的java类扩展的应用程序。 如果我从命令行类运行该程序,我可以使用以下方法实例化附加类:
Class.forName("myAddon").newInstance();
但是,如果我 jar 应用程序(正确设置主类),我会收到类未找到异常。有人能解释一下这里发生的事情吗?
TIA, 亚当
I have an application which is extendable through java classes that conform to a given interface.
If I run the program from the command line classes, I am able to instantiate the add-on classes using:
Class.forName("myAddon").newInstance();
However if I jar the application (setting the main class correctly), I get a class not found exception. Can anybody shed some light on what's going on here?
TIA,
Adam
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我已经设法使用 URLClassLoader 来完成此工作,将搜索路径指定为当前目录,如下所示:
感谢您的帮助,
Adam
I have managed to get this working using a URLClassLoader, specifying the search path to be the current directory as follows:
Thanks for all your help,
Adam
您要查找的类不在类路径上。如果它在目录中
,即
MyClass.class
MyJar.jar
您需要做:
但实际上我们需要更多信息来回答这个问题。 jar 清单中的主类设置是什么?你的类路径是什么?如何指定要加载哪个类?
The class you're looking for isn't on the classpath. If its in a directory
i.e.
MyClass.class
MyJar.jar
You need to do:
But really we need more info to answer the question. What is the main class set to in the jar manifest? What's your classpath? How do you specify which class to load?