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

时间:2014-04-12 00:46:16

标签: java swing class jar classloader

我试图根据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,它被我的捕获所有异常所捕获:

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 个答案:

答案 0 :(得分:1)

  

有人能指出我在正确的方向吗?

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

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

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

  3. 您遇到的异常发生是因为类包名称与" .class"的路径不匹配。来自classloader的根目录的文件。在最糟糕的情况下,你不能让它匹配...没有移动" .class"文件,或编写一个棘手的自定义类加载器。即使在那时,你也有(潜在的)问题,即处理其他" .class"文件。

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