[Reprinted] How JVM loads class files 2

Source: Internet
Author: User

1. all classes in Java must be loaded into JVM to run. This loading is completed by the class loader in JVM, the main task of the Class Loader is to read the class files from the hard disk to the memory.

 

2. Classes in Java are roughly divided into three types:

1. system class

2. extension class

3. Classes customized by programmers

 

3. There are two class loading methods.

1. Implicit loading. when an object is generated using the new method during the program running, the class loader is called to load the corresponding class to JVM,

2. explicitly load the required class by using methods such as class. forname ().

What is the difference between implicit loading and explicit loading? Are the two essentially the same?

 

4. Dynamic Expression of class loading

An application is always composed of N classes. When a Java program starts, it does not load all the classes at one time before running, it always loads the basic classes that ensure the program runs to the JVM at one time, and other classes are loaded when the JVM is used. This saves the memory overhead, because Java was first designed for Embedded Systems and Its memory is precious, this is an understandable mechanism, and it is also a manifestation of the dynamic nature of Java when it is loaded.

 

5. Java Class Loader

In Java, the class loader is essentially a class. The function is to load the class into JVM. It is worth noting that the JVM class loader is not one, but three. The hierarchy is as follows:

Bootstrap loader-responsible for Loading System Classes

|

--Extclassloader-responsible for loading extension classes

|

--Appclassloader-responsible for loading application classes

Why are there three class loaders? On the one hand, the division of labor is responsible for their respective blocks. On the other hand, in order to implement the delegated model, we will talk about the model below.

 

6. How to coordinate the work between class loaders

As mentioned above, there are three class loaders in Java, and the problem arises. How do they coordinate work when a class needs to be loaded, that is to say, how does Java differentiate the class loader that should be used to complete a class.

Here, Java uses the delegated model mechanism. This mechanism is simply described as "when the class loader needs to load classes, ask its parent to use its search path to help load the classes, if the parent cannot be found, it is up to you to search for the category based on your own search path. "note that this sentence is progressive.

The following is an example to illustrate how to get a better understanding of the Code:

Public class test {

Public static void main (string [] Arg ){

Classloader c = test. Class. getclassloader (); // gets the class loader of the test class

System. Out. println (C );

Classloader C1 = C. getparent (); // gets the parent class loader of class C.

System. Out. println (C1 );

Classloader C2 = c1.getparent (); // gets the parent class loader of the Class Loader C1.

System. Out. println (C2 );

}

}

Running result:

... Appclassloader...

... Extclassloader...

Null

Note :... Content omitted

We can see that test is loaded by the appclassloader loader.

The parent loader of appclassloader is extclassloader, but the parent of extclassloader is null. if you pay attention to it, we mentioned that bootstrap loader is written in C ++, from the Java point of view, the bootstrap loader class entity does not exist logically. Therefore, when we try to print out the content in the Java program code, we will see that the output is null.

 

Classloader (an abstract class) describes how a JVM loads class files.

The class loader is a component that looks for the class or interface bytecode file for parsing and constructs the JVM internal object representation. In Java, the Class Loader loads a class into the JVM by taking the following steps:

1. Load: Find and import class files

2. Link: the parsing steps are optional.

(A) Check: Check whether the data in the loaded class file is correct.

(B) Preparation: allocate storage space to static variables of the class.

(C) parsing: converting a symbolic reference into a direct reference

3. Initialization: Initialize static variables and static code blocks.

Classloder and its sub-classes are responsible for class loading. At runtime, the JVM generates three classloader: Root loader, extclassloader (Extended Class Loader), and appclassloader. The root loader is not a subclass of classloader and is compiled by C ++, therefore, he is not seen in Java and is responsible for loading the core class library of JRE, such as RT in the JRE directory. jar, charsets. jar. Extclassloader is a subclass of classloder. It is responsible for loading the jar class package under the JRE extension directory ext; appclassloader is responsible for loading the class package under the classpath path. The three class loaders have parent-child hierarchical relationships, that is, the root loader is the parent loader of extclassloader and extclassloader is the parent loader of appclassloader. By default, the class of the application is loaded using appclassloader.

The Java loading class uses the "full-responsibility delegation mechanism ". "Full responsibility" means that when a classloder loads a class, unless it is shown that another classloder is used, the class dependent on and referenced by this class is also loaded by this classloder; the "delegation mechanism" refers to entrusting the parent class loader to find the target class. It can only find and load the target class from its own class path if it cannot be found. This is from the security aspect. Imagine if a person writes a malicious basic class (such as Java. lang. string) and loaded into the JVM will cause serious consequences, but with the overall responsibility system, Java. lang. string is always loaded by the root loader to avoid the above situations

In addition to the default three class loaders of JVM, third parties can write their own class loaders to meet some special requirements. After the class file is loaded and parsed, there is a corresponding java. Lang. Class Object in JVM, providing the description of the class structure information. Arrays, enumerations, basic data types, and even void all have corresponding class objects. The class does not have a public constructor. The class object is automatically constructed by JVM by calling the defineclass () method in the Class Loader during class loading.

 

Classloader important methods:

(1) Public class <?> Loadclass (string name)

Throws classnotfoundexception

The name parameter specifies the name of the class to be loaded by the class loader. A fully qualified class name must be used. This method has an overloaded method loadclass (string name, Boolean resolve). The resolve parameter tells the Class Loader whether to parse the class. Before initializing a class, you should consider parsing the class, but not all classes need to be parsed. If the JVM only needs to know whether the class exists or find out the super class of the class, it does not need to be parsed.

(2)

 

 

[Note: most of the following content references Java's deep adventure]

After understanding the example above, go to the class-loaded delegated model instance and write two files, as shown below:

File: test1.java

Public class test1 {

Public static void main (string [] Arg ){

System. Out. println (test1.class. getclassloader ());

Test2 t2 = new Test2 ();

T2.print ();

}

}

 

File: test2.java

Public class Test2 {

Public void print (){

System. Out. println (this. getclass (). getclassloader ());

}

}

 

The roles of these two classes are to print out who is loading their class loaders and save these two files to the D: \ testclassloder directory. After compilation, We are copying them in two copies, place the files in the <JRE directory> \ Classes directory (you must create one by yourself) and <JRE directory> \ Lib \ ext \ Classes directory (no such directory, manually created), and then switch to the D: \ testclassloder directory to start the test (view the JDK version number currently in use. The directory where my JRE is located is C: \ ProgramFiles \ Java \ jdk1.6.0 \ JRE)

 

 

Test 1:

<JRE directory> \ Classes

Test1.class

Test2.class

 

<JRE directory> \ Lib \ ext \ Classes

Test1.class

Test2.class

 

Under D: \ testclassloder

Test1.class

Test2.class

 

Run the following command in DOS:

D: \ testclassloder> JAVA test1

Null

Null

 

D: \ testclassloder>

From the output, we can see that when appclassloader wants to load test1.class, it first needs its parent, that is, extclassloader, to load, and extclassloader requests its parent, that is, bootstrap loader, to load test1.class. because the <JRE directory> \ Classes directory is one of the bootstrap loader search paths, bootstrap loader finds test1.class, so it is loaded, and then there is a need to load test2.class in test1.class.
Test1.class is loaded by bootstrap loader, so test2.class is determined by bootstrap loader based on its search path. Because test2.class is also located in a path that can be found by bootstrap loader, it is also loaded, finally, we can see that both test1.class and test2.class are loaded by bootstrap loader (null.

 

If the <JRE directory> \ Lib \ ext \ Classes does not have these two class files, the results are also null.

 

Test 2:

<JRE directory> \ Classes

Test1.class

 

<JRE directory> \ Lib \ ext \ Classes

Test1.class

Test2.class

 

Under D: \ testclassloder

Test1.class

Test2.class

 

Run the following command in DOS:

D: \ testclassloder> JAVA test1

Null

Exception in thread "main" java. Lang. noclassdeffounderror: Test2 at test1.main...

D: \ testclassloder>

 

From the output, we can see that when appclassloader wants to load test1.class, it first needs its parent, that is, extclassloader, to load, and extclassloader requests its parent, that is, bootstrap loader, to load test1.class. because the <JRE directory> \ Classes directory is one of the bootstrap loader search paths, bootstrap loader finds test1.class, so it is loaded, and then there is a need to load test2.class in test1.class.
Test1.class is loaded by bootstrap loader, so the test2.class is determined by bootstrap loader based on its search path, but because bootstrap loader cannot find test2.class (we deleted it ), bootstrap loader does not have a parent, so it cannot load test2.class. finally, we can see that test1.class is loaded by bootstrap loader (null), while test2.class cannot be loaded.

 

If the <JRE directory> \ Lib \ ext \ Classes does not have these two class files, the results are the same.

 

 

Test 3

<JRE directory> \ Classes

 

Test2.class

 

<JRE directory> \ Lib \ ext \ Classes

Test1.class

Test2.class

 

Under D: \ testclassloder

Test1.class

Test2.class

 

Run the following command in DOS:

D: \ testclassloder> JAVA test1

... Extclassloader...

Null

 

D: \ testclassloder>

 

From the output, we can see that when appclassloader wants to load test1.class, it first needs its parent, that is, extclassloader, to load, and extclassloader requests its parent, that is, bootstrap loader, to load test1.class. however, bootstrap loader cannot find test1.class in its search path (we have deleted it), so extclassloader has to search for it by itself, therefore, extclassloader finds test1.class under its search path <JRE directory> \ Lib \ ext \ classes, so it is loaded, and then there is a need to load test2.class within test1.class, because test1.class is loaded by extclassloader, test2.class is determined
Extclassloader can be found based on the search path, but because extclassloader has a parent, bootstrap loader will help you find it first. test2.class is located in the path that can be found by bootstrap loader, so it is loaded by bootstrap loader. finally, we can see that test1.class is loaded by extclassloader, while test2.class is loaded by bootstrap loader (null ).

 

After learning about the above rules, ask friends to analyze the execution results of the following scenarios.

 

Test 4:

<JRE directory> \ Classes

 

<JRE directory> \ Lib \ ext \ Classes

Test1.class

Test2.class

 

Under D: \ testclassloder

Test1.class

Test2.class

 

 

Test 5:

<JRE directory> \ Classes

 

<JRE directory> \ Lib \ ext \ Classes

Test1.class

 

Under D: \ testclassloder

Test1.class

Test2.class

 

 

Test 6:

<JRE directory> \ Classes

 

<JRE directory> \ Lib \ ext \ Classes

Test2.class

 

Under D: \ testclassloder

Test1.class

Test2.class

 

 

Test 7:

<JRE directory> \ Classes

 

<JRE directory> \ Lib \ ext \ Classes

 

Under D: \ testclassloder

Test1.class

Test2.class

 

Answer:

Test 4:

... Extclassloader...

... Extclassloader...

Test 5:

... Extclassloader...

Exception in thread "Main" Java. Lang. noclassdeffounderror: Test2

At test1.main...

Test 6:

... Extclassloader...

... Appclassloader...

Test 7:

... Appclassloader...

... Appclassloader...

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.