gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 13:09:15 25 4
gpt4 key购买 nike

我正在尝试根据 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 文件,该文件将实例化不在 Eclipse 中的类。包结构(因为此时所有内容都在 .jar 内)。

有人能给我指出正确的方向吗?我还希望您能指导我在导出到 .jar 文件时如何使您的建议继续发挥作用。我意识到 Eclipse 设置一些与 .java 文件相关的 .class 文件的方式有时会把这些事情弄乱。

谢谢!

最佳答案

Could someone point me in the right direction here?

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

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

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

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

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

关于java - 使用 Class 和/或 ClassLoader 实例化 Java 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23024734/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com