哪些 类 由 JVM 加载但不是类加载器?

Which classes are loaded by JVM but not a classloader?

我正在阅读 Horstmann 的核心 Java 第 10 卷中关于断言的章节,它说:

Some classes are not loaded by a class loader but directly by the virtual machine. You can use these switches to selectively enable or disable assertions in those classes.

我对此感到困惑,classes 将由 JVM 加载而不是由 class 加载程序加载,我认为 bootstrap class 加载程序已加载最初的 classes?

谢谢。

所有 Java 虚拟机都包含一个嵌入在虚拟机中的 class 加载程序。这个嵌入式加载器被称为原始 class 加载器。它有些特殊,因为虚拟机假定它可以访问受信任 classes 的存储库,VM 可以 运行 无需验证。

因此,如果我们编写如下代码:

Class r = loadClass(String className, boolean resolveIt); 

这意味着 class 在 运行 时间由 classLoader 加载,JVM 负责执行程序。

希望你明白了!

根据 Oracle 文档,没有 class 加载程序的系统 classes 引用位于 rt.jar 并由 [=17= 加载的所有 classes ] class装载机。因此,您无法访问上述系统的 ClassLoader 对象 classes.

The bootstrap class loader loads the system classes (typically, from the JAR file rt.jar ). It is an integral part of the virtual machine and is usually implemented in C. There is no ClassLoader object corresponding to the bootstrap class loader. For example,

String.class.getClassLoader()

returns null.

最后,我们应该使用 -enablesystemassertions/-esa 开关在系统 classes 中启用断言。