I have a pretty sizable application (one that uses several utility JARs) that I would now like to instantiate a multiple number of times within the same JVM. Unfortunately, many parts of the app (some not under my control) use class variables for their operation.
Is it possible to reuse the system classloader (the same one that can load classes from JARs and already knows the classpath) so that I can place each instance of my app in its own sandbox?
Not really. If you want two different copies of the same class in your JVM (so you can keep their static variables separated and so on) then you will have to have them loaded by two different classloaders. If you try to have them both loaded by the system classloader then there aren't any shenanigans you can pull to make them appear different.
Can I create multiple instances of the system classloader and use those separate instances to create my separate apps? I wouldn't want to write a custom ClassLoader that does the same job of reading classes from JAR files.
The class loader delegation model is such that even if you write your own class loader, its parent, presumably the system class loader, will be allowed first to try to load the class.
Therefore, your classes should not be on the classpath!
If the above is not a problem for you, use instances of URLClassLoader, which allow you to specify the file or jar locations of the classes as URL's. Jar URL's look like this: "jar://...".
Edited by: baftos on Sep 30, 2010 2:35 PM