0

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.

user1625108
  • 91
  • 1
  • 5

1 Answers1

0

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.

auselen
  • 27,577
  • 7
  • 73
  • 114
  • Also please see this answer, http://stackoverflow.com/questions/60764/how-should-i-load-jars-dynamically-at-runtime – auselen Jan 12 '13 at 00:42