简体   繁体   中英

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 . 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. The program then throws a NoClassDefFoundError caused by 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

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. I solved this problem by loading all classes in the jar file. I used the method here to find all of the classes in the jar file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM