Java Class 加载中
Java Class loading
我想了解 Java class 加载器的工作原理。看了一些文章,还是有些不明白。
据我了解,第一个 class 加载程序是 Bootstrap class 加载程序 (BCL)。 是JVM加载的吗?
之后,BCL 加载 rt.jar 库,扩展 Class 加载程序 (ECL)。
反过来,ECL 加载扩展和应用程序 Class 加载程序 (ACL)。 ACL负责从class路径加载所有用户开发的class。
这个描述是否正确?
有一些问题:
- 内存中每个 classloader 只存在一个实例? (BCL,ECL,ACL) ?
- 我看过委托原则,但对我来说有点不清楚。它是如何工作的,假设我们需要加载 MyClass。首先 jvm 将此 class 名称赋予 ACL,这对我来说还不清楚,ACL 通过 class 路径查找,如果没有这样的 class 则将此工作委托给父级或它调用后立即将此工作委托给父级,我的意思是 JVM 将 class 名称赋予 ACL,它不会搜索此 class 将其赋予 ECL,此 CL 反过来也不做任何工作并将它交给 BCL,只有当 BCL 找不到这个 class 它 return 它到较低级别(ECL)....等等。什么是正确的链条?
- 当我们创建自定义 class 加载器时,它的父 class 是什么?应用程序 Class 加载程序?我们可以指定 ,例如 ECL。至于 class 加载器的层次结构不是继承,我们在构造函数中指定父级。我们能否获取 ECL classloader 的实例以在我们的自定义 CL 中将其指定为构造函数中的父级。
- 为什么 class 像 String 、 Object 等等 return 没有任何 ClassLoadder ?
答案:
As far as I understand the first class loader is the Bootstrap class loader (BCL). Is it loaded by the JVM?
这没有明确定义,但很可能会用本机代码编写。
Afterwards, the BCL loads rt.jar library, and the Extension Class Loader (ECL).
是的,它会加载 rt.jar(直到 Java 8 和 Java 9 一样会有新的模块系统)。是否加载ECL没有明确定义。
In turn, the ECL loads extensions and the Application Class Loader (ACL). The ACL is responsible for loading all user-developed class from the classpath.
是的,它加载扩展,是否加载 ACL 没有明确定义。 ACL 确实加载了 class 路径条目。
There is only one instance of each classloader exists in memory ? (BCL,ECL,ACL) ?
是的,这是正确的。由于class身份定义为一对FQCN及其有效的classloader,否则无法正常工作。
I have looked at delegation principle,but it is a little bit unclear for me. How does it work, let's assume we need load MyClass. First jvm gives this class name to the ACL, and here is unclear thing for me, ACL looks through class path and if there is no such class delegates this work to the parent OR it delegates this work to parent right after calling, I mean JVM gives class name to ACL, it doesn't search for this class gives it to ECL, this CL in turn also doesn't do any work and gives it to BCL, and only if BCL cannot find this class it return it to the lower level (ECL) .... and so on. What is the correct chain ?
在标准Java中,有父优先委派模型,这意味着classloader会先询问它的parent,然后才会尝试自己加载一个class。
When we creating custom class loader what is parent class of it ? Application ClassLoader ? Can we specify , for instance ECL. As far as hierarchy of class loaders is not inheritance, we specify parent in constructor. Can we get instance of ECL classloader to specify it in our custom CL as a parent in constructor.
自定义 classloader 的默认父级是应用程序 classloader.
问题是您为什么需要这样做?您的程序不太可能正确运行。正式地,您可以通过 YourClass.class.getClassLoader().getParent()
.
Why classes like String , Object and so on don't return any ClassLoadder ?
Bootstrap classloader在API.
[=49中表示为null
=]
我想了解 Java class 加载器的工作原理。看了一些文章,还是有些不明白。
据我了解,第一个 class 加载程序是 Bootstrap class 加载程序 (BCL)。 是JVM加载的吗?
之后,BCL 加载 rt.jar 库,扩展 Class 加载程序 (ECL)。
反过来,ECL 加载扩展和应用程序 Class 加载程序 (ACL)。 ACL负责从class路径加载所有用户开发的class。
这个描述是否正确?
有一些问题:
- 内存中每个 classloader 只存在一个实例? (BCL,ECL,ACL) ?
- 我看过委托原则,但对我来说有点不清楚。它是如何工作的,假设我们需要加载 MyClass。首先 jvm 将此 class 名称赋予 ACL,这对我来说还不清楚,ACL 通过 class 路径查找,如果没有这样的 class 则将此工作委托给父级或它调用后立即将此工作委托给父级,我的意思是 JVM 将 class 名称赋予 ACL,它不会搜索此 class 将其赋予 ECL,此 CL 反过来也不做任何工作并将它交给 BCL,只有当 BCL 找不到这个 class 它 return 它到较低级别(ECL)....等等。什么是正确的链条?
- 当我们创建自定义 class 加载器时,它的父 class 是什么?应用程序 Class 加载程序?我们可以指定 ,例如 ECL。至于 class 加载器的层次结构不是继承,我们在构造函数中指定父级。我们能否获取 ECL classloader 的实例以在我们的自定义 CL 中将其指定为构造函数中的父级。
- 为什么 class 像 String 、 Object 等等 return 没有任何 ClassLoadder ?
答案:
As far as I understand the first class loader is the Bootstrap class loader (BCL). Is it loaded by the JVM?
这没有明确定义,但很可能会用本机代码编写。
Afterwards, the BCL loads rt.jar library, and the Extension Class Loader (ECL).
是的,它会加载 rt.jar(直到 Java 8 和 Java 9 一样会有新的模块系统)。是否加载ECL没有明确定义。
In turn, the ECL loads extensions and the Application Class Loader (ACL). The ACL is responsible for loading all user-developed class from the classpath.
是的,它加载扩展,是否加载 ACL 没有明确定义。 ACL 确实加载了 class 路径条目。
There is only one instance of each classloader exists in memory ? (BCL,ECL,ACL) ?
是的,这是正确的。由于class身份定义为一对FQCN及其有效的classloader,否则无法正常工作。
I have looked at delegation principle,but it is a little bit unclear for me. How does it work, let's assume we need load MyClass. First jvm gives this class name to the ACL, and here is unclear thing for me, ACL looks through class path and if there is no such class delegates this work to the parent OR it delegates this work to parent right after calling, I mean JVM gives class name to ACL, it doesn't search for this class gives it to ECL, this CL in turn also doesn't do any work and gives it to BCL, and only if BCL cannot find this class it return it to the lower level (ECL) .... and so on. What is the correct chain ?
在标准Java中,有父优先委派模型,这意味着classloader会先询问它的parent,然后才会尝试自己加载一个class。
When we creating custom class loader what is parent class of it ? Application ClassLoader ? Can we specify , for instance ECL. As far as hierarchy of class loaders is not inheritance, we specify parent in constructor. Can we get instance of ECL classloader to specify it in our custom CL as a parent in constructor.
自定义 classloader 的默认父级是应用程序 classloader.
问题是您为什么需要这样做?您的程序不太可能正确运行。正式地,您可以通过
YourClass.class.getClassLoader().getParent()
.Why classes like String , Object and so on don't return any ClassLoadder ?
Bootstrap classloader在API.
[=49中表示为null
=]