Note that there are some explanatory texts on larger screens.

plurals
  1. POChange classloader
    text
    copied!<p>I'm trying to switch the class loader at runtime:</p> <pre><code>public class Test { public static void main(String[] args) throws Exception { final InjectingClassLoader classLoader = new InjectingClassLoader(); Thread.currentThread().setContextClassLoader(classLoader); Thread thread = new Thread("test") { public void run() { System.out.println("running..."); // approach 1 ClassLoader cl = TestProxy.class.getClassLoader(); try { Class c = classLoader.loadClass("classloader.TestProxy"); Object o = c.newInstance(); c.getMethod("test", new Class[] {}).invoke(o); } catch (Exception e) { e.printStackTrace(); } // approach 2 new TestProxy().test(); }; }; thread.setContextClassLoader(classLoader); thread.start(); } } </code></pre> <p>and:</p> <pre><code>public class TestProxy { public void test() { ClassLoader tcl = Thread.currentThread().getContextClassLoader(); ClassLoader ccl = ClassToLoad.class.getClassLoader(); ClassToLoad classToLoad = new ClassToLoad(); } } </code></pre> <p>(<em>InjectingClassLoader</em> is a class extending the <em>org.apache.bcel.util.ClassLoader</em> which should load the modified versions of classes before asking it's parent for them)</p> <p>I'd like to make the result of "approach 1" and "approach 2" exactly same, but it looks like <em>thread.setContextClassLoader(classLoader)</em> does nothing and the "approach 2" always uses the system classloader (can be determined by comparing tcl and ccl variables while debugging).</p> <p>Is it possible to make <strong>all</strong> classes loaded by new thread use given classloader?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload