无法在jar文件中加载类

时间:2014-11-01 18:57:58

标签: java plugins classloader

我目前正在开发一个旨在接受插件的项目。为了从他们的jar文件加载插件,我使用的是ClassLoader。这是我用来加载插件的代码。

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();

当我运行程序时,我能够加载我创建的测试插件,但是稍后在程序中,我要求插件执行一个操作,该操作使用最初未使用的插件jar中的类。然后程序会抛出由NoClassDefFoundError

引起的ClassNotFoundException
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

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

我弄清楚问题是什么。加载类msciplugin.PluginDriver时,URLClassLoader未加载所需的所有其他类。我通过加载jar文件中的所有类来解决这个问题。我使用方法here来查找jar文件中的所有类。