简体   繁体   English

Java 9 中的类加载器层次结构

[英]Classloaders hierarchy in Java 9

As of Java 8, I know the hierarchy of the classloaders has been as follows:从 Java 8 开始,我知道类加载器的层次结构如下:

Bootstrap classloader → Extension classloader → Application classloader引导类加载器 → 扩展类加载器 → 应用类加载器

What is the change in the hierarchy of classloaders in Java 9 and how does it work? Java 9 中类加载器的层次结构有何变化以及它是如何工作的?

Here is the migration guide for Java 9,这是 Java 9 的迁移指南

New Class Loader Implementations新的类加载器实现

JDK 9 maintains the hierarchy of class loaders that existed since the 1.2 release . JDK 9 维护自 1.2 版本以来存在的类加载器的层次结构 However, the following changes have been made to implement the module system:但是,为了实现模块系统,进行了以下更改:

The application class loader is no longer an instance of URLClassLoader but, rather, of an internal class.应用程序类加载器不再是 URLClassLoader 的实例,而是内部类的实例 It is the default loader for classes in modules that are neither Java SE nor JDK modules.它是既不是 Java SE 也不是 JDK 模块的模块中类的默认加载器。

The extension class loader has been renamed ;扩展类加载器重命名 it is now the platform class loader .它现在是平台类加载器 All classes in the Java SE Platform are guaranteed to be visible through the platform class loader. Java SE 平台中的所有类都保证通过平台类加载器可见。 In addition, the classes in modules that are standardized under the Java Community Process but not part of the Java SE Platform are guaranteed to be visible through the platform class loader.此外,在 Java Community Process 下标准化但不属于 Java SE 平台的模块中的类保证通过平台类加载器可见。

Just because a class is visible through the platform class loader does not mean the class is actually defined by the platform class loader.仅仅因为一个类通过平台类加载器可见并不意味着该类实际上是由平台类加载器定义的。 Some classes in the Java SE Platform are defined by the platform class loader while others are defined by the bootstrap class loader. Java SE 平台中的某些类由平台类加载器定义,而其他类由引导类加载器定义。 Applications should not depend on which class loader defines which platform class.应用程序不应依赖于哪个类加载器定义了哪个平台类。

The changes in JDK 9 may impact code that creates class loaders with null (that is, the bootstrap class loader) as the parent class loader and assumes that all platform classes are visible to the parent. JDK 9 中的更改可能会影响创建以 null(即引导类加载器)作为父类加载器的类加载器的代码,并假设所有平台类对父类都是可见的。 Such code may need to be changed to use the platform class loader as the parent (see ClassLoader.getPlatformClassLoader).可能需要更改此类代码以使用平台类加载器作为父级(请参阅 ClassLoader.getPlatformClassLoader)。

The platform class loader is not an instance of URLClassLoader , but, rather, of an internal class.平台类加载器不是 URLClassLoader 的实例,而是内部类的实例

The bootstrap class loader is still built-in to the Java Virtual Machine and represented by null in the ClassLoader API.引导类加载器仍然内置于 Java 虚拟机中,并在 ClassLoader API 中由 null 表示。 It defines the classes in a handful of critical modules, such as java.base.它定义了一些关键模块中的类,例如 java.base。 As a result, it defines far fewer classes than in JDK 8, so applications that are deployed with -Xbootclasspath/a or that create class loaders with null as the parent may need to change as described previously.因此,它定义的类比 JDK 8 中的类少得多,因此使用 -Xbootclasspath/a 部署的应用程序或创建以 null 作为父级的类加载器的应用程序可能需要如前所述进行更改。

The ClassLoader as revised in Java-9 states that: Java-9 中修订的ClassLoader指出:

The Java run-time has the following built-in class loaders: Java 运行时具有以下内置类加载器:

  • Bootstrap class loader : The virtual machine's built-in class loader typically represented as null, and does not have a parent. Bootstrap class loader :虚拟机的内置类加载器通常表示为 null,并且没有父类。

  • Platform class loader : To allow for upgrading/overriding of modules defined to the platform class loader, and where upgraded modules read modules defined to class loaders other than the platform class loader and its ancestors, then the platform class loader may have to delegate to other class loaders, the application class loader for example. Platform class loader :为了允许升级/覆盖定义给平台类加载器的模块,并且升级的模块读取定义给除平台类加载器及其祖先之外的类加载器的模块,然后平台类加载器可能必须委托给其他类加载器,例如应用程序类加载器。 In other words, classes in named modules defined to class loaders other than the platform class loader and its ancestors may be visible to the platform class loader .换句话说,为平台类加载器及其祖先以外的类加载器定义的命名模块中的类可能对平台类加载器可见

  • System class loader : It is also known as application class loader and is distinct from the platform class loader. System class loader也称为应用类加载器,区别于平台类加载器。 The system class loader is typically used to define classes on the application class path, module path, and JDK-specific tools .系统类加载器通常用于在应用程序类路径、模块路径和 JDK 特定工具上定义类 The platform class loader is a parent or an ancestor of the system class loader that all platform classes are visible to it.平台类加载器是系统类加载器的父级或祖先,所有平台类对它都是可见的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM