如何更改 Java 中的默认类加载器?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6366288/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 15:36:17  来源:igfitidea点击:

How to change default class loader in Java?

javaclassclassloader

提问by Yifan

Let's say I have three classes, example.ClassA, example.ClassB, and example.ClassLoader. ClassA prints out HelloWorld and ClassB imports example.ClassA and invokes its main() method. If I do this:

假设我有三个类,example.ClassA、example.ClassB 和 example.ClassLoader。ClassA 打印出 HelloWorld 并且 ClassB 导入 example.ClassA 并调用其 main() 方法。如果我这样做:

java -cp Example.jar -Djava.system.class.loader=example.ClassLoader example.ClassA

java -cp Example.jar -Djava.system.class.loader=example.ClassLoader example.ClassA

It works and uses my class loader. However, if I do this:

它可以工作并使用我的类加载器。但是,如果我这样做:

java -cp Example.jar -Djava.system.class.loader=example.ClassLoader example.ClassB

java -cp Example.jar -Djava.system.class.loader=example.ClassLoader example.ClassB

ClassB uses my class loader, but ClassA (which was imported by ClassB) is loaded using a default class loader.

ClassB 使用我的类加载器,但 ClassA(由 ClassB 导入)是使用默认类加载器加载的。

Is there any way to force Java to always use my class loader (unless another class loader is specified explicitly)?

有什么方法可以强制 Java 始终使用我的类加载器(除非明确指定了另一个类加载器)?

EDIT: Thanks to Pa?lo Ebermann's answer below, I figured the problem is that I'm calling the parent class loader (URLClassLoader) to load the classes that I don't need to touch, and those loaded classes set that as it's context class loader, so classes imported from it uses the parent class loader of my custom loader. (confusing, sorry) Now I can get it to work by manually reading in EVERY class, however it seems redundant as I've copied URLClassLoader's code directly. Is there a way to tell the parent class loader to find and define the class, BUT set the Class's context class loader to your custom one?

编辑:感谢 Pa?lo Ebermann 在下面的回答,我认为问题是我正在调用父类加载器 (URLClassLoader) 来加载我不需要触摸的类,而那些加载的类将其设置为上下文类加载器,所以从它导入的类使用我的自定义加载器的父类加载器。(令人困惑,抱歉)现在我可以通过在每个类中手动读取来让它工作,但是它似乎是多余的,因为我直接复制了 URLClassLoader 的代码。有没有办法告诉父类加载器查找和定义类,但是将类的上下文类加载器设置为您的自定义类加载器?

采纳答案by Pa?lo Ebermann

If your class loader is implemented right, it will first ask its parent class loader about any classes that should be loaded.

如果你的类加载器实现正确,它会首先询问它的父类加载器是否应该加载任何类。

The parent class loader of your loader will likely be the usual application class loader. This means that every class your class loader loads will first searched on the application class loader, and only if not found, on your one.

加载器的父类加载器可能是通常的应用程序类加载器。这意味着您的类加载器加载的每个类将首先在应用程序类加载器中搜索,并且只有在未找到时才会在您的类加载器中搜索。

All classes which are defined by your class loader will also search their needed classes on your classloader. If they don't do, your ClassA is not really loaded by your loader.

由您的类加载器定义的所有类也将在您的类加载器上搜索它们需要的类。如果他们不这样做,您的 ClassA 并没有真正被您的加载器加载。

If this does not help, you will need to show some code on how you got to your results.

如果这没有帮助,您将需要显示一些有关如何获得结果的代码。



An idea on what to do:

关于做什么的想法:

class ModifyingClassLoader extends URLClassLoader {

    // TODO: add constructors

    private boolean needsModifying(String name) {
        // TODO
    }

    private byte[] modifyClass(InputStream original) throws IOException {
        // TODO
    }

    public Class<?> findClass(String name) throws {
        if(needsModifying(name)) {
            try {
                InputStream classData = getResourceAsStream(name.replace('.', '/') + ".class");
                if(classData == null) {
                    throw new ClassNotFoundException("class " + name + " is not findable");
                }
                byte[] array = modifyClass(classData);
                return defineClass(name, array, 0, array.length);
            }
            catch(IOException io) {
                throw new ClassNotFoundException(io);
            }
        }
        else {
            return super.findClass(name);
        }
    }
}

To your question:

对于你的问题:

Is there a way to tell the parent class loader to find and define the class, BUT set the Class's context class loader to your custom one?

有没有办法告诉父类加载器查找和定义类,但是将类的上下文类加载器设置为您的自定义类加载器?

No. The ClassLoader of a class is always the one whose defineClassmethod was called to create the class. (The context class loaderis something else - it is thread specific, and only used by classes which explicitly want to use it, not by classes resolving their own direct dependencies.)

不是。类的 ClassLoader 总是defineClass调用其方法来创建类的那个。(上下文类加载器是别的东西——它是线程特定的,并且只由明确想要使用它的类使用,而不是由解决它们自己的直接依赖关系的类使用。)