简体   繁体   English

jvm是否加载类路径提到的所有类?

[英]Does jvm load all the classes mentioned by the classpath?

When we invoke java command with -cp command then we provide some directories and jar files. 当我们用-cp命令调用java命令时,我们提供了一些目录和jar文件。 Does jvm load all the classes mentioned by the classpath Or it is just a super set of all classes which jvm will look up to load when required? jvm会加载类路径中提到的所有类吗?还是只是在需要时jvm会加载的所有类的超集?

Does jvm load all the classes mentioned by the classpath Or it is just a super set of all classes which jvm will look up to load when required? jvm会加载类路径中提到的所有类吗?还是只是在需要时jvm会加载的所有类的超集?

JVM loads classes form the classpath on the need basis ie when a reference is found for the class, it is loaded. JVM根据需要从类路径中加载类,即,当找到该类的引用时,将对其进行加载。 Also there is a hierarchy of class loaders in JVM, a class loaded by the parent class loader is used by the lower class loaders. JVM中也有一个类加载器的层次结构,下级类加载器使用由父类加载器加载的类。

There are two concepts involved 涉及两个概念

  • loading 装货
  • initialization 初始化

Initializing a class will initialize fields and execute static blocks. 初始化类将初始化字段并执行静态块。 The exact moment this happens is important to application semantics, therefore it is precisely defined . 发生的确切时刻对于应用程序语义很重要,因此它是精确定义的

Initialization requires loading first; 初始化需要先加载; but loading is more of an internal concept of the JVM. 但是加载更多是JVM的内部概念。 JVM could, and is allowed to, aggressively preload classes even if unneeded. 即使不需要,JVM也可以并且被允许积极地预加载类。 This process does not affect the application semantics and it is invisible to the application. 此过程不会影响应用程序的语义,并且对应用程序是不可见的。

As far as the application is concerned, a class must have been loaded if we get a Class object of it, eg from Foo.class , Class.forName , or other reflection APIs. 就应用程序而言,如果我们从Foo.classClass.forName或其他反射API获得Class对象,则必须已加载该类。 We can examine the properties of the Class without necessarily triggering initialization. 我们可以检查Class的属性,而不必触发初始化。

One important constraint - we must get the same Class object for the same class name (and from the same classloader). 一个重要的约束-我们必须为相同的类名(和从相同的类加载器)获得相同的Class对象。 The Class object is the representation of the loaded class. Class对象是已加载类表示。

  1. JVM loads only the referenced classes and not every class present in the jars in your classpath JVM仅加载引用的类,而不加载类路径中jar中存在的每个类
  2. Loading of classes happens through a hierarchical manner 类的加载通过分层方式进行
  3. Having too many jars in the classpath only occupies more disk space 类路径中有太多jar只会占用更多磁盘空间
  4. JVM only uses memory in your physical memory(RAM). JVM仅使用物理内存(RAM)中的内存。

Loading of class occurs in order and looks in locations. 类的加载按顺序进行,并在位置中查找。 -cp falls under third category as listed below .Mostly the application classes should be supplied through -cp or it will look for the enviroment variable CLASSPATH. -cp属于下面列出的第三类。大多数应用程序类应通过-cp提供,否则它将寻找环境变量CLASSPATH。

The extension framework makes use of the class-loading delegation mechanism. 扩展框架利用了类加载委托机制。 When the runtime environment needs to load a new class for an application, it looks for the class in the following locations, in order: 当运行时环境需要为应用程序加载新类时,它将在以下位置按顺序查找该类:

1)Bootstrap classes: the runtime classes in rt.jar, internationalization classes in i18n.jar, and others. 1)Bootstrap类: rt.jar中的运行时类,i18n.jar中的国际化类等。

2)Installed extensions: classes in JAR files in the lib/ext directory of the JRE, and in the system-wide, platform-specific extension directory (such as /usr/jdk/packages/lib/ext on the Solaris™ Operating System, but note that use of this directory applies only to Java™ 6 and later). 2)已安装的扩展: JRE的lib / ext目录中以及系统范围内特定于平台的扩展目录(例如Solaris™操作系统上的/ usr / jdk / packages / lib / ext 中JAR文件中的类,但请注意,此目录的使用仅适用于Java™6和更高版本。

3)The class path: classes, including classes in JAR files, on paths specified by the system property java.class.path. 3)类路径:类,包括JAR文件中的类,位于系统属性java.class.path指定的路径上。 If a JAR file on the class path has a manifest with the Class-Path attribute, JAR files specified by the Class-Path attribute will be searched also. 如果类路径上的JAR文件具有带有Class-Path属性的清单,则还将搜索由Class-Path属性指定的JAR文件。 By default, the java.class.path property's value is ., the current directory. 默认情况下,java.class.path属性的值为当前目录.。 You can change the value by using the -classpath or -cp command-line options, or setting the CLASSPATH environment variable. 您可以使用-classpath或-cp命令行选项或设置CLASSPATH环境变量来更改值。 The command-line options override the setting of the CLASSPATH environment variable. 命令行选项将覆盖CLASSPATH环境变量的设置。

https://docs.oracle.com/javase/tutorial/ext/basics/load.html https://docs.oracle.com/javase/tutorial/ext/basics/load.html

It doesn't load all the classes, but it knows where to look for them when you need them. 它不会加载所有类,但是它知道在需要它们时在哪里寻找它们。

They will be loaded when they are first needed. 第一次需要它们时将被加载。

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

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