Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何用Java加载插件类?_Java_Plugins_Casting_Classloader - Fatal编程技术网

如何用Java加载插件类?

如何用Java加载插件类?,java,plugins,casting,classloader,Java,Plugins,Casting,Classloader,我制作了一个小应用程序,它从文件夹中加载类,将它们显示在列表中,当您单击其中一个类时,它会在其中显示JPanel 所以我制作了一个API类(BaseApp),它扩展了JPanel。我还制作了一个测试类,它扩展了BaseApp,并使用了其中的API 现在,我的问题是每次我点击TestApp,它都会给我这个异常 Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: java.lang.Class cannot

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

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

现在,我的问题是每次我点击
TestApp
,它都会给我这个
异常

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)
我试图将我从类加载器获得的
转换为
BaseApp
。我怎样才能不让它崩溃呢

如果您需要,这是我的
文件类加载器

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;
    }
}

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

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

您的注释是一个回复。如果答案有助于你解决问题,请接受
Object c = cl.findClass(new File(***CLASS FILE LOCATION***));
System.out.println(c);
switchApp((BaseApp) c);