gpt4 book ai didi

java自定义类加载器: some classes are not loaded by my classloader

转载 作者:太空宇宙 更新时间:2023-11-04 08:43:42 24 4
gpt4 key购买 nike

我正在编写一个自定义类加载器,我已使用参数将其设置为默认类加载器

-Djava.system.class.loader=MyClassLoader

我的类加载器加载了大部分类,但有些类却没有加载,为什么?这些类位于外部 jar 文件中。

更新这是一个例子

public class Main{
public static void main(String[] args) {
try{
// A simple class loader, ovveride loadClass
// method and print in stdout the name of the class loaded.
MyClassLoader classLoader=new MyClassLoader(MyClassLoader.class.getClassLoader());
Class init=classLoader.loadClass("Initializer");
Object instance=init.newInstance();
init.getMethod("init").invoke(instance);
}
catch(Exception ex){
ex.printStackTrace();
}
}
}

public class A{

public A() {
System.out.println("Im A");
}
}

public class Initializer {

public void init() {
A a=new A();
}
}

问题是:我期望类 A 由我的类加载器加载,但这并没有发生,为什么?

更新

无论如何,我想用我的类加载器加载所有类,因为我想加密类代码并在运行时解密它。那么,我如何使用我的类加载器作为我所有类的默认类加载器?

谢谢。

最佳答案

java.lang 下的任何内容都将始终由引导类加载器加载。

来自http://en.wikipedia.org/wiki/Java_Classloader :

When the JVM is started, three class loaders are used[3][4]:

  1. Bootstrap class loader
  2. Extensions class loader
  3. System class loader

The bootstrap class loader loads the core Java libraries[5] (/lib directory). This class loader, which is part of the core JVM, is written in native code.

The extensions class loader loads the code in the extensions directories (/lib/ext or any other directory specified by the java.ext.dirs system property). It is implemented by the sun.misc.Launcher$ExtClassLoader class.

关于java自定义类加载器: some classes are not loaded by my classloader,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4776456/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com