I'm developing a simple Plugin System and I basically want to control which classloader loads a class on a new
instruction.
For instance I have a jar with my main program, and I distribute a class Foo.class
in a extra packaged jar called Bar.jar
.
I see three different possibilities to include the jar in my main program:
Bar.jar
with my main jar to include it in the classpath. This is obviously not feasible for a plugin systemFoo
with an URL ClassLoader pointing to the jar. This would be too unfamiliar with the default java programmer to load classes manually via its name-Djava.system.class.loader
argument. But I dont really want to force the user to start his application with extra arguments.Is there another way? Maybe something to register a Custom classloader for specific classes? or change the Classloader new
uses at Runtime? Setting the ContextClassLoader
of the Thread
does not work.
When you load a class, the classes it refers to use the same class loader as it. This means to control the class loader used implicitly, you can to use a different class loader for the one which starts it all. e.g. Your first Runnable.
class A {
B b = new B();
The new B
will chose the ClassLoader used for A (or a parent)
Setting the context class loader works for dynamic lookups like Class.forName(name);
is the same as Class.forName(Thread.currentThread().getContextClassLoader(), name);
Once a class is loaded it will always use the same classes it depends on. It's not possible to swap different classes based on say a thread context. This means a class with a very specific classloader can use classes which are more generally used (e.g. as the general class is from a parent), however a more general used class cannot use a more specific class loader it is not aware of. i.e. ClassLoaders are not aware of their children.
One reason for this is that threads share memory. This means you can't say that when A creates a new B in one thread it will be different to another thread as the A instance could be accessed by either thread.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With