使用class和/或ClassLoader实例化Java类



我正试图根据JFileChooser返回给我的文件实例化一个类。因此,到目前为止,我已经使JFileChooser工作,并且我可以成功地获取我正在查找的.class文件。然而,我在实际使用.class文件并用它创建对象时遇到了问题

这是我正在写的方法。。。

public static Agent loadAgentFromFile(File file){
        Agent agent = null;
        String fileName = file.getName();
        String filePath = file.getAbsolutePath();
        String parentDirectory = file.getParentFile().getName();
        String agentClassName = fileName.substring(0, fileName.lastIndexOf('.'));
        Object object = null;
        //System.out.println(filePath.contains(parentDirectory));
        //System.out.println(filePath.substring(0, filePath.indexOf(parentDirectory)));
        try {
            File f = new File(filePath.substring(0, filePath.indexOf(parentDirectory)));
            URL[] cp = {f.toURI().toURL()};
            URLClassLoader urlcl = new URLClassLoader(cp);
            Class agentDefinition = null;
            if (parentDirectory.equals("Agents"))
            {
                System.out.println(PACKAGE + agentClassName);
                agentDefinition = urlcl.loadClass(PACKAGE + agentClassName);
                //agentDefinition = Class.forName(PACKAGE + agentClassName);
            }
            else
            {
                System.out.println(PACKAGE + parentDirectory + "." + agentClassName);
                agentDefinition = urlcl.loadClass(parentDirectory + "." + agentClassName);
            }
            object = agentDefinition.newInstance();
            agent = (Agent)object;
        }
        catch (InstantiationException e) {
            JOptionPane.showMessageDialog(THIS,
                "The chosen class must not be an interface or be abstract, " +
                "and it must not have any arguments in its constructor.");
        }
        catch (IllegalAccessException e) {
            JOptionPane.showMessageDialog(THIS,
                "The chosen class's constructor must be public.");
        }
        catch (ClassNotFoundException e) {
            JOptionPane.showMessageDialog(THIS,
                "The chosen class cannot be found.  Make sure the .java file is compiled.");
        }
        catch (ClassCastException e) {
            JOptionPane.showMessageDialog(THIS,
                "The chosen class does not extend from the Agent class.");
        }
        catch (Exception e) {
            e.printStackTrace(); //prints the stack to see what exception occurred that we didn't catch
            JOptionPane.showMessageDialog(THIS,
                "The chosen class does not conform to the "Agent" class.");
        }
        return agent;
    }

然而,当这个方法被调用时,我实际上得到了一个NoClassDefFoundError,它被我的catch-all Exception捕获:

Exception in thread "AWT-EventQueue-0" java.lang.NoClassDefFoundError: TestAgents/TestDFSAgent (wrong name: WumpusEnvironment/Model/Agent/TestAgents/TestDFSAgent)
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(Unknown Source)
    at java.security.SecureClassLoader.defineClass(Unknown Source)
    at java.net.URLClassLoader.defineClass(Unknown Source)
    at java.net.URLClassLoader.access$100(Unknown Source)
    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.lang.ClassLoader.loadClass(Unknown Source)
    at WumpusEnvironment.Model.Agent.AgentLoader.loadAgentFromFile(AgentLoader.java:41)
    at WumpusEnvironment.View.MainWindow.ApplicationWindow.actionPerformed(ApplicationWindow.java:270)
    at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
    at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    at javax.swing.AbstractButton.doClick(Unknown Source)
    at javax.swing.plaf.basic.BasicMenuItemUI.doClick(Unknown Source)
    at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mouseReleased(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$200(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)

我看过以前的同类答案,最初我的.class文件位于整个包结构之外,所以我移动了它们。我的类文件现在和源文件在同一个文件夹中,但我仍然收到这个错误。可能需要注意的是,我正试图在Eclipse中运行这一点,尽管将来整个项目将导出到一个.jar文件中,该文件将实例化不在包结构中的类(因为此时这些都在.jar中)。

有人能在这里给我指正确的方向吗?当我导出到.jar文件时,我也很感激关于如何使您的建议继续有效的建议。我意识到,Eclipse相对于.java文件设置一些.class文件的方式有时会把这些事情搞砸。

谢谢!

有人能在这里给我指正确的方向吗?

我认为正确的方向是不要这样做。我建议你:

  1. 将插件代理类放入JAR文件中(在JAR中使用正确的路径)。

  2. 为每个JAR文件创建单独的类加载器,或者为所有JAR文件创建一个类加载器。。。这取决于代理类是否需要彼此隔离。

您遇到的异常是因为classes包名称与classloader根目录中".class"文件的路径不匹配。在最坏的情况下,你无法匹配。。。而不需要移动".class"文件,也不需要编写一个自定义的类加载器来执行一些棘手的操作。即便如此,在处理对其他".class"文件的需要或不需要的依赖关系时,您也会遇到(潜在的)问题。

将插件放入JAR可以避免这些问题。

最新更新