[英]How do i load a plugin class with Java?
I made a small application that loads classes from a folder, displays them in a list and when you click on one it displays the JPanel
in it. 我创建了一个小应用程序,它从一个文件夹中加载类,将它们显示在一个列表中,当你点击它时,它会在其中显示JPanel
。
So i made an API class ( BaseApp
) which extends JPanel
. 所以我创建了一个扩展JPanel
的API类( BaseApp
)。 I also made a test class which extends BaseApp
and uses the APIs in it. 我还创建了一个测试类,它extends BaseApp
并使用了API。
Now, my problem is that every time i click on TestApp
, it gives me this Exception
... 现在,我的问题是,每次我点击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)
I have tried to cast the Class
i got from my classloader into a BaseApp
. 我曾试图投的Class
从我的类加载器我陷入了BaseApp
。 How do i do that without it crashing? 如果没有它崩溃,我该怎么做?
If you need it, here's my FileClassLoader
: 如果你需要它,这是我的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;
}
}
Also, this is where the casting occurs: 此外,这是铸造发生的地方:
Object c = cl.findClass(new File(***CLASS FILE LOCATION***));
System.out.println(c);
switchApp((BaseApp) c);
An instance of type Class
is not an instance of type BaseApp
, so the conversion fails. 类型Class
的实例不是BaseApp
类型的实例,因此转换失败。 You need to instantiate the class to get an instance of type BaseClass
, which you can then cast 您需要实例化该类以获取BaseClass
类型的实例,然后您可以将其强制转换
Try calling newInstance()
on the class instance to get a new instance of the loaded class (based on its default constructor) 尝试在类实例上调用newInstance()
以获取已加载类的新实例(基于其默认构造函数)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.