2

现在,在我开始之前 - 我是 Java 新手,而且我有点陷入了困境。

我有一个系统,我可以在其中以插件的形式从 jar 文件将类加载到我的应用程序中:

URLClassLoader loader = new URLClassLoader(urlList);
// urlList is a 1 slice URL[] with a single URL to a jar file in it
Class<?> toolClass = Class.forName(className, true, loader);
// className is a String with the main class name in the jar

这一切都很好而且很花哨——我可以在那个加载的类中调用方法,而且一切正常。

现在,我希望能够在该 jar 文件中存储各种资源并让类能够访问它们。我一直在看 getResourceAsStream(),它看起来应该是要走的路:

InputStream in = this.getClass().getClassLoader().getResourceAsStream("path/to/my/test.txt");

...除了它总是返回null。我一直在关注有关此功能的示例和其他论坛帖子等,但似乎没有一个与我的具体情况有关:

  1. jar 文件在运行时使用 URLClassLoader 加载。
  2. jar 文件不在 classPath 中。

那么我在这些情况下尝试做的事情实际上可能吗,还是我必须求助于手动解压缩 jar 文件来获取资源?(老实说,我不想那样做。)


编辑:这是我正在使用的功能:

加载一个jar文件:

public void loadPlugin(File jar)
{
    try {
        URL[] urlList = new URL[1];
        urlList[0]  = jar.toURI().toURL();

        URLClassLoader loader = new URLClassLoader(urlList);

        String className = null;
        className = findClassInZipFile(jar); // This just walks the zip file looking for the class

        if (className == null) {
            return;
        }

        JarFile jf = new JarFile(jar);
        Manifest manifest = jf.getManifest();
        Attributes manifestContents = manifest.getMainAttributes();

        Map pluginInfo = new LinkedHashMap();
        pluginInfo.put("version", manifestContents.getValue("Version"));
        pluginInfo.put("compiled", manifestContents.getValue("Compiled"));
        pluginInfo.put("jarfile", jar.getAbsolutePath());

        Class<?> toolClass;
        try {
            toolClass = Class.forName(className, true, loader);
        } catch (Exception ex) {
            ex.printStackTrace();
            return;
        }
        Tool tool = (Tool) toolClass.newInstance();

        // If the setInfo method doesn't exist we don't care.
        try {
            tool.setInfo(pluginInfo);
        } catch (Exception blah) {
        }
        plugins.put(className, tool);
    } catch (Exception e) {
        System.err.println(e.getMessage());
    }
}

结果类中的函数:

public void run() {
    try {
        InputStream in = this.getClass().getClassLoader().getResourceAsStream("uecide/app/tools/test.txt");
        if (in == null) {
            System.err.println("FAIL!!!");
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

jar 文件如下所示:

   0 Fri Jun 28 16:52:32 BST 2013 META-INF/
 140 Fri Jun 28 16:52:30 BST 2013 META-INF/MANIFEST.MF
   0 Fri Jun 28 16:52:32 BST 2013 uecide/
   0 Fri Jun 28 16:52:32 BST 2013 uecide/app/
   0 Fri Jun 28 16:52:32 BST 2013 uecide/app/tools/
1880 Fri Jun 28 16:52:32 BST 2013 uecide/app/tools/ExportToMPLABX.class
2754 Fri Jun 28 16:52:32 BST 2013 uecide/app/tools/test.txt
4

1 回答 1

1

正如所发现的,问题在于新的类加载器首先委托给父类加载器,它正在寻找类。

选项:

  • 确保当前的类加载器没有您要加载的类
  • null构造新的时指定父类加载器引用(可能?)URLClassLoader
于 2013-06-28T17:38:29.470 回答