Java 类别及;类装载器

Java 类别及;类装载器,java,classloader,Java,Classloader,我有一个自定义类加载器:CustomClassLoader(扩展类加载器) 我有一个类:IntegerPrint 我用我的自定义类加载器加载我的类。我希望下面代码中的SOP返回相同的值。但是第一个SOP打印了“sun.misc.Launcher”$AppClassLoader@..“&第二次SOP打印”CustomClassLoader@.." 为什么会这样?请告知 public class IntegerPrinterTest { public static void main(Str

我有一个自定义类加载器:CustomClassLoader(扩展类加载器)

我有一个类:IntegerPrint

我用我的自定义类加载器加载我的类。我希望下面代码中的SOP返回相同的值。但是第一个SOP打印了“sun.misc.Launcher”$AppClassLoader@..“&第二次SOP打印”CustomClassLoader@.."

为什么会这样?请告知

public class IntegerPrinterTest {
    public static void main(String[] args) throws Exception {
        CustomClassLoader loader = new CustomClassLoader(IntegerPrinterTest.class.getClassLoader());
        Class<?> clazz = loader.loadClass("IntegerPrinter");
        System.out.println(IntegerPrinter.class.getClassLoader());
        System.out.println(clazz.getClassLoader());
    }
}
公共类IntegerPrinterTest{
公共静态void main(字符串[]args)引发异常{
CustomClassLoader=新的CustomClassLoader(IntegerPrinterTest.class.getClassLoader());
Class clazz=loader.loadClass(“整型打印机”);
System.out.println(IntegerPrinter.class.getClassLoader());
System.out.println(clazz.getClassLoader());
}
}
第一次呼叫:

IntegerPrinter.class.getClassLoader()
实际上可以做到:

IntegerPrinterTest.class.getClassLoader().loadClass("IntegerPrinter")
所以它完全忽略了自定义类加载器。 换句话说:您自己的类加载器实际上并不用于您使用诸如“new”等本机调用创建的任何对象。为此,它还应负责加载IntegerPrinter类

在同一课堂上做这件事是相当谨慎的(通常是无用的),但你可以做到:

Class<?> clazz = loader.loadClass("IntegerPrinterTest");
clazz.getMethod("main").invoke(null);
Class clazz=loader.loadClass(“IntegerPrinterTest”);
getMethod(“main”).invoke(null);
(注意:此代码未经测试,但应近似于正常工作的代码)

第一次调用:

IntegerPrinter.class.getClassLoader()
实际上可以做到:

IntegerPrinterTest.class.getClassLoader().loadClass("IntegerPrinter")
所以它完全忽略了自定义类加载器。 换句话说:您自己的类加载器实际上并不用于您使用诸如“new”等本机调用创建的任何对象。为此,它还应负责加载IntegerPrinter类

在同一课堂上做这件事是相当谨慎的(通常是无用的),但你可以做到:

Class<?> clazz = loader.loadClass("IntegerPrinterTest");
clazz.getMethod("main").invoke(null);
Class clazz=loader.loadClass(“IntegerPrinterTest”);
getMethod(“main”).invoke(null);
(请注意,此代码未经测试,但应近似于正常工作的代码)

您期望得到什么? 在

您创建了一个

 Class<IntegerPrint> 
查看哪些类是按什么顺序排列的。

您期望的是什么? 在

您创建了一个

 Class<IntegerPrint> 
查看哪些类按什么顺序排列