Recently I have been studying osgi. There is a very important thing in osgi: classloader. So I collected some information online and sorted it out,
And added some understanding;
(1) JVM loading process and loading principle
The so-called loading is the process of searching for the binary form of a class or an interface and using the binary form to construct the class object representing this class or this interface,
The class or interface name is given. Of course, the name can also be obtained through computation, but more often it is obtained by searching the source code after compilation by the compiler.
. In Java, the Class Loader loads a class into a Java Virtual Machine. Three steps are required: Loading, linking, and initialization,
The link can be divided into three steps: verification, preparation, and resolution. In addition to resolution, the other steps are completed in strict order. The main work of each step is as follows:
Load: Query and import binary data of classes or interfaces;
Link: perform the following verification, preparation, and resolution steps. The resolution steps are optional;
Verify: Check whether the binary data of the import class or interface is correct;
Preparation: allocate and initialize the storage space for static variables of the class;
Resolution: convert a symbolic reference to a direct reference;
Initialization: Initialize the Java code and static Java code block for activating static variables of the class.
(2): What is the class in Java?
A class indicates the code to be executed, and data indicates its related status. The status changes frequently, but the Code does not. When we compare a specific state with a class, it means to make a class case. Although instances of the same class have different statuses, they all correspond to the same code. In Java, a class usually has a. Class file, but there are exceptions. In the Java Runtime Environment (Java runtime), each class has a Java object with the first class as the code that appears. It is Java. lang. class instance. We compile a Java file, and the compiler will embed a public file,
Static, final modifier type is Java. Lang. Class, the domain variable named class is in its bytecode file. Because the public modifier is used, we can access it in the following form:
Java. Lang. Class Klass = myclass. Class;
Once a class is loaded into JVM, the same class will not be loaded again (Remember, the same class ). There is a problem here: what is "the same class "? Just as an object has a specific State, that is, identifier, an object is always associated with its code (class. Similarly, the classes loaded into JVM also have a specific Identifier. Let's take a look.
In Java, a class uses its fully-matched Class Name (fully qualified class name) as its identifier. Here, the fully-matched class name includes the package name and class name. However, in JVM, a class uses its full name and an instance that loads classloader as a unique identifier. Therefore, if a package named PG contains a class named Cl that is loaded by an instance kl1 of the Class Loader klassloader, that is, c1.class is expressed as (CL, PG, kl1) in JVM ). This means that the instances (CL, PG, kl1) and (CL, PG, kl2) of the two class loaders are different, so the classes loaded by them are completely different, incompatible. So how many types of loaders are there in the JVM? In the next section, we will reveal the answer.
(3): Java classloader:
In Java, we can obtain the following three classloader classes:
I. Basic concepts of classloader
1. classloader category
The class loader is used to load a class into JVM.
The JVM Specification defines two types of class loaders: Start the internal loader (bootstrap) and user-defined loader (user-defined class loader ).
At runtime, the JVM generates three classloader: Bootstrap classloader, extension classloader, and appclassloader. Bootstrap is written in C ++. We cannot see it in Java. It is null. It is a class loader that comes with JVM and is used to load core class libraries, such as Java. Lang.
The parent of appclassloader is extclassloader, while the parent of extclassloader is Bootstrap classloader.
Java provides the abstract class classloader. All user-defined class loaders instantiate the class of the class loader. System class loader is a special user-defined class loader provided by the JVM implementer. It is loaded by default when the programmer does not specify a loader. The system class loader can be obtained through the classloader. getsystemclassloader () method.
Example 1: Test the classloader of the JVM
/* Loadersample1.java */
Public class loadersample1 {
Public static void main (string [] ARGs ){
Class C;
Classloader Cl;
CL = classloader. getsystemclassloader ();
System. Out. println (CL );
While (CL! = NULL ){
CL = Cl. getparent ();
System. Out. println (CL );
}
Try {
C = Class. forname ("Java. Lang. Object ");
CL = C. getclassloader ();
System. Out. println ("Java. Lang. Object's loader is" + Cl );
C = Class. forname ("loadersample1 ");
CL = C. getclassloader ();
System. Out. println ("loadersample1's loader is" + Cl );
} Catch (exception e ){
E. printstacktrace ();
}
}
}
Running result on my machine (Sun Java 1.4.2)
Sun. Misc. launcher $ appclassloader @ 1a0c10f
Sun. Misc. launcher $ extclassloader @ e2eec8
Null
Java. Lang. Object's loader is null
Loadersample1's loader is
Sun. Misc. launcher $ appclassloader @ 1a0c10f
The first line indicates that the system class loader is instantiated from the sun. Misc. launcher $ appclassloader class.
The second line indicates that the parent of the system class loader is instantiated from the sun. Misc. launcher $ extclassloader class.
The third line indicates that the parent of the system class loader is Bootstrap.
The fourth line indicates that the core class java. Lang. object is loaded by bootstrap.
The fifth line indicates that the user class loadersample1 is loaded by the system class loader.
Note: we can clearly see the parent-child relationship (not an inheritance relationship) between the three classloader classes. The parent-child relationship has a classloader type attribute in the implementation of classloader, we can initialize the definition when implementing the custom classloader, and the parent-child relationship of the three system-defined classloader is
Appclassloader ------ (parent) extclassloader ---------- (parent) bootclassloader (null C ++ implementation)
Why should the system specify so many classloader classes?
The answer is that Java dynamically loads classes. In this way, memory can be saved and what to load is used, however, when the system is running, it does not know our application and what classes need to be loaded. Therefore, this step-by-step loading method is used.
(1) first load the core API to make the system run in the most basic way.
(2) LOAD extension class
(3) Load user-defined classes
Package org. Corey. clsloader;
Import java.net. malformedurlexception;
Import java.net. url;
Import java.net. urlclassloader;
Import sun.net. SPI. nameservice. DNS. dnsnameservice;
Public class clsloaderdemo {
/**
* @ Param ARGs
*/
Public static void main (string [] ARGs ){
System. Out. println (system. getproperty ("Sun. boot. Class. Path "));
System. Out. println (system. getproperty ("Java. Ext. dirs "));
System. Out. println (system. getproperty ("Java. Class. Path "));
}
}
Program result:
E: \ myeclipse 6.0 \ JRE \ Lib \ RT. jar; E: \ myeclipse 6.0 \ JRE \ Lib \ i18n. jar; E: \ myeclipse 6.0 \ JRE \ Lib \ sunrsasign. jar; E: \ myeclipse 6.0 \ JRE \ Lib \ JSSE. jar; E: \ myeclipse 6.0 \ JRE \ Lib \ JCE. jar; E: \ myeclipse 6.0 \ JRE \ Lib \ charsets. jar; E: \ myeclipse 6.0 \ JRE \ Classes
E: \ myeclipse 6.0 \ JRE \ Lib \ ext
E: \ workspace \ classloaderdemo \ bin
In the above results, you can clearly see that the three classloader load the class paths separately. You also know why when we compile the program, we need to put the jar package used under the classpath of the project. We also know why we can not load Java. lang. * package! Java. Lang. * Is In The Rt. jar package;
(4) classloader loading mechanism:
Now we design this demo:
Package java.net;
Public class URL {
Private string path;
Public URL (string path ){
This. Path = path;
}
Public String tostring (){
Return this. Path + "New Path ";
}
}
Package java.net;
Import java.net .*;
Public class thesameclsdemo {
/**
* @ Param ARGs
*/
Public static void main (string [] ARGs ){
URL url = new URL ("http://www.baidu.com ");
System. Out. println (URL. tostring ());
}
}
Original text from [than the Internet], reproduced Please retain the original link: http://soft.chinabyte.com/database/6/11330006.shtml