[英]Unable to load classes in a jar file
I am currently working on a project that is designed to accept plugins. 我目前正在从事一个旨在接受插件的项目。 In order to load the plugins from their jar file, I am using a
ClassLoader
. 为了从其jar文件加载插件,我使用了
ClassLoader
。 This is the code that I am using to load the plugins. 这是我用来加载插件的代码。
URLClassLoader loader = URLClassLoader.newInstance(new URL[] {pluginJar.toURI().toURL()}, this.getClass().getClassLoader());
try {
Class<?> clazz= Class.forName("msciplugin.PluginDriver", true, loader);
Constructor<?> constructor=clazz.getConstructor(ProgramManager.class);
MSCIPlugin plugin=(MSCIPlugin) constructor.newInstance(programManager);
plugins.add(plugin);
plugin.addMSCIPluginListener(this);
this.pluginAdded(plugin);
} catch (Exception e) {
e.printStackTrace();
return false;
}
loader.close();
When I run the program, I am able to load the test plugin that I created, but later in the program, I ask the plugin to perform an operation, which uses a class from the plugin jar that was not used initially. 当我运行程序时,我能够加载我创建的测试插件,但是稍后在程序中,我要求插件执行一项操作,该操作使用了插件jar中最初没有使用的类。 The program then throws a
NoClassDefFoundError
caused by ClassNotFoundException
然后程序抛出由
ClassNotFoundException
引起的NoClassDefFoundError
Exception in thread "AWT-EventQueue-0" java.lang.NoClassDefFoundError: msciplugin/TimeUtils
at msciplugin.ServerControllerConfigurationWindow.refreashEditor(ServerControllerConfigurationWindow.java:195)
at msciplugin.ServerControllerConfigurationWindow.valueChanged(ServerControllerConfigurationWindow.java:299)
at javax.swing.JList.fireSelectionValueChanged(Unknown Source)
at javax.swing.JList$ListSelectionHandler.valueChanged(Unknown Source)
at javax.swing.DefaultListSelectionModel.fireValueChanged(Unknown Source)
at javax.swing.DefaultListSelectionModel.fireValueChanged(Unknown Source)
at javax.swing.DefaultListSelectionModel.fireValueChanged(Unknown Source)
at javax.swing.DefaultListSelectionModel.changeSelection(Unknown Source)
at javax.swing.DefaultListSelectionModel.changeSelection(Unknown Source)
at javax.swing.DefaultListSelectionModel.setSelectionInterval(Unknown Source)
at javax.swing.JList.setSelectionInterval(Unknown Source)
at javax.swing.plaf.basic.BasicListUI$Handler.adjustSelection(Unknown Source)
at javax.swing.plaf.basic.BasicListUI$Handler.mousePressed(Unknown Source)
at java.awt.AWTEventMulticaster.mousePressed(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$400(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
Caused by: java.lang.ClassNotFoundException: msciplugin.TimeUtils
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.net.FactoryURLClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 45 more
Any help would be greatly appreciated. 任何帮助将不胜感激。
I figured out what the problem was. 我发现了问题所在。 When loading the class
msciplugin.PluginDriver
the URLClassLoader
was not loading all other classes that would be needed. 加载
msciplugin.PluginDriver
类msciplugin.PluginDriver
, URLClassLoader
并未加载所有其他需要的类。 I solved this problem by loading all classes in the jar file. 我通过在jar文件中加载所有类来解决此问题。 I used the method here to find all of the classes in the jar file.
我在这里使用了该方法来查找jar文件中的所有类。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.