简体   繁体   中英

What classloader does “new” use, and how do i change it?

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:

  1. package Bar.jar with my main jar to include it in the classpath. This is obviously not feasible for a plugin system
  2. Load the class Foo 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
  3. Set a custom Classloader as the System-classloader via the -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. eg 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 (eg 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. ie 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM