我制作了一个小应用程序,它从文件夹中加载类,在列表中显示它们,当你点击一个文件夹时,它会在其中显示JPanel
。
所以我制作了一个扩展BaseApp
的API类(JPanel
)。我还制作了一个extends 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);
答案 0 :(得分:2)
类型Class
的实例不是BaseApp
类型的实例,因此转换失败。您需要实例化该类以获取类型为BaseClass
的实例,然后您可以将其转换为
尝试在类实例上调用newInstance()
以获取已加载类的新实例(基于其默认构造函数)