gpt4 book ai didi

java - 如何使用 Java 加载插件类?

转载 作者:行者123 更新时间:2023-12-02 07:45:38 31 4
gpt4 key购买 nike

我制作了一个小型应用程序,它从文件夹加载类,将它们显示在列表中,当您单击其中一个时,它会在其中显示 JPanel

所以我创建了一个扩展JPanel的API类(BaseApp)。我还创建了一个测试类,它扩展 BaseApp 并使用其中的 API。

现在,我的问题是,每次我单击 TestApp 时,它都会给我这个 Exception...

Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: java.lang.Class     cannot be cast to me.Delocaz.SuperApp.BaseApp
at me.Delocaz.SuperApp.AppSelect$2.mouseClicked(AppSelect.java:76)
at java.awt.AWTEventMulticaster.mouseClicked(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$000(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 转换为BaseApp。我该如何做到这一点而不崩溃?

如果您需要它,这是我的FileClassLoader:

package me.Delocaz.SuperApp;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class FileClassLoader extends ClassLoader {
public Class<?> findClass(File f) {
byte[] b = loadClassData(f);
try {
return defineClass(null, b, 0, b.length);
} catch (ClassFormatError ex) {
return null;
}
}
private byte[] loadClassData(File f) {
FileInputStream fin;
byte fileContent[] = new byte[(int)f.length()];
try {
fin = new FileInputStream(f);
fin.read(fileContent);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return fileContent;
}
}

此外,这也是转换发生的地方:

Object c = cl.findClass(new File(***CLASS FILE LOCATION***));
System.out.println(c);
switchApp((BaseApp) c);

最佳答案

Class 类型的实例不是 BaseApp 类型的实例,因此转换失败。您需要实例化该类以获取 BaseClass 类型的实例,然后可以对其进行强制转换

尝试在类实例上调用newInstance()以获取已加载类的新实例(基于其默认构造函数)

关于java - 如何使用 Java 加载插件类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10903119/

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