Test case below. Output:
custom loading of: pkg.TestRun
ran.
wish this would run in my custom classloader
What I want to happen is to see “custom loading of: pkg.TestRun” show up between the second and third lines of the output.
How can I get it to load the dependencies of a class from my custom classloader, even when the first class is loaded from the parent? Note, that in my real case, I get a class not found exception because the equivalent of OtherClass is not known to the parent classloader.
I know one solution is to have the custom class loader explicitly load TestRun. However, how to load TestRun is already known to the parent classloader and I don’t want to have to manage finding it separately since it’s already done and it might be tricky for me to somehow figure that out when it’s already being managed without me doing anything. And I’ve tried to do something like super.getResource (returns null) or findClass (already sets parent as classloader for it) but neither worked.
So, can I let the parent find the class, but the custom loader define it? Or, is there just a way to make it so that it will always use my custom loader to look for dependencies?
package pkg;
public class TestCL {
static class MyCL extends ClassLoader {
MyCL(ClassLoader parent) {
super(parent);
}
@Override
public Class loadClass(String name, boolean resolve) throws ClassNotFoundException {
System.out.println("custom loading of: " + name);
return getParent().loadClass(name);
}
}
public static void main(String[] args) throws Exception {
MyCL cl = new MyCL(Thread.currentThread().getContextClassLoader());
Thread.currentThread().setContextClassLoader(cl);
cl.loadClass("pkg.TestRun").getMethod("run", new Class[] {}).invoke(null);
}
}
class TestRun {
public static void run() {
System.out.println("ran.");
OtherClass.runAlso();
}
}
class OtherClass {
public static void runAlso() {
System.out.println("wish this would run in my custom classloader");
}
}
One solution is to use the parent classloader (or the one that already knows where it is) to read the bytes of the class, then define it in the custom class loader.
So, something like this:
Then you can call
defineClass(name, bytes, 0, bytes.length)with those bytes to define it in the custom class loader.