Load jar file contained within another jar file no

2019-09-16 02:12发布

问题:

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.