Runtime class loading
807569Jul 14 2006 — edited Jul 28 2006I'm working on an application and I need to do something kind of tricky. The classes needed to run will vary depending on what a user selects in the gui. If one thing is selected use these classes, if the other thing is selected use these other classes. The worst part of all this is that the jar files that contain the different classes interfere with each other. So if I put all of the jars in the classpath it stops working.
So what I'd like to do is use the URLClassLoader, if possible. So in my code I have this:
File root = new File(System.getProperty("user.dir"),"lib");
File[] jars = root.listFiles();
URL[] urls = new URL[jars.length];
for(int i=0;i<jars.length;i++) {
urls[i] = jars.toURL();
}
loader = URLClassLoader.newInstance(urls,Main.class.getClassLoader());
In the File(System.getProperty("user.dir"),"lib") directory I put the jar files and it seems to be loading properly. But now what? Do I specifically load each class before I can use it using loader.loadClass()?