0

我在运行时在java中调用类时遇到问题我基本上是在制作一个插件框架

它首先打开 plugin/Plugins.cfg 并将测试解析为地图.. cfg 文件中的 EX 文本 1 = myplugin 2 = plugin2

(每个插件的主类是:plugin.(plugin name).main.class)

如您所见,它从地图中加载每个值并尝试运行其主类

public static void loadPlugins()
{
    int x = hackers.core.startup.InitializeGame.map.size();
    for (int i = 1; i<=x;i++)
    {
        String className = hackers.core.startup.InitializeGame.map.get(i + "");

        File file  = new File(System.getProperty("user.dir") + File.separator + "plugins" + File.separator + className);
        URL url = null;
        try {
            url = file.toURI().toURL();
        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }  
        URL[] urls = new URL[]{url};
        ClassLoader cl = new URLClassLoader(urls);

        try {
            Class cls = cl.loadClass("plugin." + className + ".main");
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        System.out.println(className);
    }
}

Class cls = cl.loadClass("plugin." + className + ".main");

^line 给了我错误:java.lang.ClassNotFoundException: plugin.myplugin.main

有人知道这里有什么问题吗?或任何建议,我已经查看了它的 API,但它让我感到困惑并且缺乏文档。

4

1 回答 1

0

file没有指向有效的类或 jar 文件。

调试以下行;您会注意到它的路径不是文件系统中的现有路径。

File file  = new File(System.getProperty("user.dir") + File.separator + "plugins" + File.separator + className);

我会假设您忘记包含.classclassName.

于 2012-10-04T01:05:25.350 回答