Load jar file contained within another jar file not working

谁说我不能喝 提交于 2019-12-25 09:47:30

问题


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();

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.


回答1:


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.

See JarClassLoader tutorial.



来源:https://stackoverflow.com/questions/14288842/load-jar-file-contained-within-another-jar-file-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!