1

If i create a custom class loader for say 3(A,B,C) classes of my Java app and redirect all other loading to default bootstrap class loader. Then according to JVM spec : "A reference to a class is consulted from current class loader and loaded (if necessary ) accordingly"

Now if i try to create object of class D(whose Class-Loader reference is bootstrap in "Class" class) from class A(whose Class-Loader reference is custom loader) , then custom-loader does not have any way to find path of D.class file (say i pick A,B,C from internet / some other place in which case , CLASSPATH is different in custom loader).

So how does this class finding problem would be resolved by JVM?

4

1 回答 1

3

如果你正确地实现你的自定义类加载器,就不会有问题。所有类加载器都有一个父级。对于自定义类加载器,它通常是系统类加载器,即从 Java 类路径加载类的系统类加载器。通常,类加载器首先提供(委托)其父级以加载一个类,并且只有当父级失败时,它才会尝试加载该类。为了实现这一点,自定义类加载器需要实现findClass方法,并且委托将由 ClassLoader.loadClass 自动完成。

请注意,JVM 不仅具有引导类加载器,而且具有三个类加载器的层次结构

引导 <- 扩展 <- 系统

于 2013-07-26T13:14:23.663 回答