简体   繁体   中英

How to make Java use my classloader for class resolution

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");
    }
}

The problem was you were not loading anything using your custom classloader
It asks it parent to load the class, so everything was being loaded by the main classloader.

Try this modified code:

package stackoverflow.june2012.classloader;

import java.io.File;
import java.net.URL;
import java.net.URLClassLoader;

public class TestCL {

static class MyCL extends URLClassLoader {
    MyCL(URL[] urls) {
        // NOTE: No parent classloader!
        super(urls, null);
    }

    @Override
    public Class loadClass(String name, boolean resolve) throws ClassNotFoundException {
        System.out.println("custom loading of: " + name);
        return super.loadClass(name, resolve);
    }

    @Override
    protected Class<?> findClass(String name) throws ClassNotFoundException {
        System.out.println("findClass: " + name);
        System.out.println("NOTE: Only called if this classloader does NOT have a parent");
        return super.findClass(name);
    }
}

public static void main(String[] args) throws Exception {
    URL url = new File("./bin").toURI().toURL();
    System.out.println("url to search for classes: " + url);
    MyCL cl = new MyCL(new URL[] {url});
    Thread.currentThread().setContextClassLoader(cl);
    Class loadClass = cl.loadClass("stackoverflow.june2012.classloader.TestRun");
    System.out.println("Loaded TestRun using classloader: " + loadClass.getClassLoader());
    loadClass.getMethod("run", new Class[] {}).invoke(null);
}

}

And I had to move your other 2 classes into a separate file, otherwise it cant be accessed as it was package-private in a different classloader:

package stackoverflow.june2012.classloader;

public 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:

public static byte[] loadBytesForClass(ClassLoader loader, String fqn) throws IOException {
    InputStream input = loader.getResourceAsStream(fqn.replace(".", "/") + ".class");
    if (input == null) {
        System.out.println("Could not load bytes for class: " + fqn);
    }
    ByteArrayOutputStream output = new ByteArrayOutputStream();
    StringUtil.copy(input, output);
    return output.toByteArray();
}

Then you can call defineClass(name, bytes, 0, bytes.length) with those bytes to define it in the custom class loader.

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