加载另一个jar文件中包含的jar文件不起作用 [英] Load jar file contained within another jar file not working

查看:114
本文介绍了加载另一个jar文件中包含的jar文件不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在动态地加载jar文件,但无法正常工作。我在Eclipse的源文件夹中有一个jar文件(resource.jar),所以它位于类路径中。我正在尝试将其作为加载小程序,将其添加到jframe并运行它的资源。对于我自己来说,这出于某种未知原因是行不通的。这是我正在尝试的代码。

I've been playing around with loading jar files dynamically and I can't get this to work. I have a jar file (resource.jar) in my source folder of Eclipse so it's in the classpath. I'm trying to get it as a resource to load an applet, add it to a jframe, and run it. This isn't working for some unknown reason to myself. This is the code I'm trying.

URL jarURL = getClass().getClassLoader().getResource("resource.jar");
ClassLoader urlLoader = new URLClassLoader(new URL[]{jarURL});
applet = (Applet) urlLoader.loadClass("test.TestClassApplet").newInstance();
jframe.add(applet);
applet.init();
applet.start();

当我尝试获取资源时没有错误,错误是在加载类时出现的。我得到一个ClassNotFoundException,即使该类IS在jar文件中。

I get no error when I try to get the resource, the error is when I load the class. I get a ClassNotFoundException, even though the class IS in the jar file.

推荐答案

URLClassLoader设计用于加载通过搜索一组URL访问的类和资源。它不会为您提取jar文件中的类。

URLClassLoader is designed to be used for loading classes and resources that are accessed by searching a set of URLs. It won't extract the class in the jar file for you.

请参见 JarClassLoader 教程。

这篇关于加载另一个jar文件中包含的jar文件不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆