Class Loader classes load process.

Source: Internet
Author: User
Class loading is the first step in the Java program, and the study of class loading helps to understand the JVM execution process and to guide developers to take more effective measures to match the execution of the program. The second goal of the research class loading mechanism is to enable the program to dynamically control class loading, such as thermal deployment, to improve the flexibility and adaptability of the program. first, the simple processJava programs run in memory, when executed at the command line: Java HelloWorld command, the JVM will load Helloworld.class into memory, and form a class object Helloworld.class. The process is the class loading process: 1, look for the JRE directory, look for Jvm.dll, and initialize the JVM, 2, generate a Bootstrap Loader (start class loader), 3, Bootstrap Loader automatically load extended Loader (Standard extended class loader) and sets its parent Loader to Bootstrap Loader. 4. Bootstrap Loader automatically loads Appclass Loader (System class loader) and sets its parent Loader to extended Loader. 5, finally by Appclass Loader load HelloWorld class. The above is the most general process of class loading. two, the class loader each search directory

To figure this out, first take a look at the API Doc document for the System class. 1. Bootstrap Loader (Start class loader): Loads the path or jar specified by System.getproperty ("Sun.boot.class.path"). 2, Extended Loader (Standard extended class loader Extclassloader): Load the path or jar specified by System.getproperty ("Java.ext.dirs"). When you run a program using Java, you can also specify its search path, for example: Java-djava.ext.dirs=d:/projects/testproj/classes HelloWorld 3, Appclass Loader (System class loader Appclassloader): Loads the path or jar specified by System.getproperty ("Java.class.path"). When you run a program using Java, you can also add-CP to overwrite the original classpath settings, such as: JAVA-CP./lavasoft/classes HelloWorld After the JVM starts, Extclassloader and Appclassloader save a copy in the JVM and cannot change its search path while the program is running. If you want to load a class from another search path at run time, a new classloader will be generated.third, the characteristics of the class loader1. When running a program, the specified class is always loaded by the Appclass Loader (System class loader). 2. When loading a class, each class loader will give the load task to its parent, if the parent cannot find it, and then load it by itself.
3. The Bootstrap Loader (boot class loader) is the top-level ClassLoader and its parent loader is null.Iv. acquisition of Class loadersIt's easy to see the following example public class HelloWorld {
public static void Main (string[] args) {
HelloWorld Hello = new HelloWorld ();
Class C = Hello.getclass ();
ClassLoader loader = C.getclassloader ();
SYSTEM.OUT.PRINTLN (loader);
System.out.println (Loader.getparent ());
System.out.println (Loader.getparent (). GetParent ());
}
Printing results: sun.misc.launcher$appclassloader@19821f
Sun.misc.launcher$extclassloader@addbf1
Null

Process finished with exit code 0 as you can see from the results above, you do not get the parent Loader of Extclassloader, because Bootstrap Loader (boot ClassLoader) is implemented in C language, Cannot find a certain way to return to the parent loader, and then returns NULL.v. Class loadingClass loading There are three ways: 1, the command line to start the application by the JVM initialization load 2, through the Class.forName () method dynamically loaded 3, through the Classloader.loadclass () method dynamically loading three different ways, see an example to understand: public class HelloWorld {
public static void Main (string[] args) throws ClassNotFoundException {
ClassLoader loader = HelloWorld. Class.getclassloader ();
SYSTEM.OUT.PRINTLN (loader);
Use Classloader.loadclass () to load the class without executing the initialization block
Loader.loadclass ("Test2");
Using Class.forName () to load the class, the initialization block is executed by default
Class.forName ("Test2");
Use Class.forName () to load the class and specify ClassLoader to initialize without executing a static block
Class.forName ("Test2", false, loader);
}
} public class Test2 {
static {
SYSTEM.OUT.PRINTLN ("Static initialization block executed.") ");
}
Toggle the load mode separately, there will be different output results.Vi. Custom ClassLoaderTo illustrate the problem, first look at the example: Package test;

Import java.net.MalformedURLException;
Import Java.net.URL;
Import Java.net.URLClassLoader;

/**
* Custom ClassLoader
*
* @author leizhimin 2009-7-29 22:05:48
*/
public class Myclassloader {
public static void Main (string[] args) throws Malformedurlexception, ClassNotFoundException, Illegalaccessexception, instantiationexception {
URL url = new URL ("File:/e://projects//testscanner//out//production//testscanner");
ClassLoader Myloader = new URLClassLoader (new Url[]{url});
Class C = myloader.loadclass ("Test.") Test3 ");
System.out.println ("----------");
Test3 t3 = (Test3) c.newinstance ();
}
} public class Test3 {
static {
The static initialization block of the SYSTEM.OUT.PRINTLN ("Test3") was executed. ");
}
After running:----------
The static initialization block for the TEST3 was executed.

Process finished with exit code 0 shows that the custom ClassLoader Myloader = new URLClassLoader (New Url[]{url}) has been successfully loaded into memory by the class Test3, and the The default construction method constructs the object Test3 t3 = (Test3) c.newinstance (); One important point about ClassLoader is that the same class file loaded with the same ClassLoader has only a single class instance. However, if the same class file is loaded with a different classloader, there will be two different ClassLoader instances (provided that two class loaders cannot use the same parent class loader).

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.