Java class Loader

Source: Internet
Author: User

1. Introduction to the system loader

Multiple ClassLoader can be installed in a Java Virtual machine, the system defaults to three main class loaders (BootStrap, Extclassloader, Appclassloader), and each classloader is responsible for loading classes at a particular location.

The ClassLoader itself is also a Java class (except for Bootstrap), because it is also loaded by the ClassLoader itself, so it is clear that the first ClassLoader is not a Java class, and yes, it is the bootstrap class loader. It is written in the C + + language, embedded in the Java Virtual Machine kernel of the class loader, when the Java VM is started, it is loaded.

2. Structure and jurisdiction of class loaders

All ClassLoader in a Java Virtual machine are organized in a tree structure with a parent-child relationship. When instantiating a ClassLoader object, you need to specify a parent class loader object for it, or it will be loaded by default with the System class loader for its parent class. The tree-like structure of the classloader and its jurisdictional scope are as follows:


  


3. Delegate mechanism of class loader

When a Java virtual machine loads a class, which class does it send to load? The following principles are followed when loading:

Principle 1: First Send the class loader of the current thread to load class

Principle 2: Each class loader loads a class and then delegates it to its parent loader first. When all ancestor loaders are not loaded into the class, only the initiator loader is returned. If it has not yet been loaded into the class, ClassNotFoundException will be thrown. Will not go to the initiator loader's son, because there is no Getchild method, even if there are, so many sons (the parent class only one), which one to find?

Principle 3: If Class A refers to Class B, the Java Virtual machine uses the loader that loads class A to load Class B.

Principle 4: You can also specify a loader directly to load classes, such as: Classloader.loadclass ().

Note that each classloader can only load classes in a specific location and directory, respectively. But they can delegate other ClassLoader to load the class, which is the class loader's delegate mode. The ClassLoader is delegated to the Bootstrap class loader at the first level, and when Bootstrap cannot load the class that is currently being loaded, it is then returned to the descendant loader at the first level to load the class. When it returns to the original ClassLoader, it throws a ClassNotFound exception if it cannot complete the loading of the class itself.

4. Example 1

First we define an empty class Testclassloader, the code is as follows:

Package Com.tgb.clazzloaders;public class Testclassloader {}

Then we define a test class Testmain to output the class loader name of the Testclassloader class:

Package Com.tgb.clazzloaders;public class Testmain {public static void main (string[] args) throws Exception {//Output class Testcla Ssloader when the name of the class loader is System.out.println (TestClassLoader.class.getClassLoader (). GetClass (). GetName ());}}

The output is as follows,$AppClassLoader for Sun.misc.Launcher:


  


We then put the class Testclassloader into a jar package and place it in the current Jre\lib\ext directory, such as:


  


Running the test class Testmain again, we can be surprised to see the output turned into a sun.misc.Launcher$ExtClassLoader. this validates the ClassLoader delegation mechanism above us. When the Testclassloader class is loaded, the current ClassLoader is delegated to the parent loader level one level, and then back to Extclassloader , it Jre\lib\ext\*.jarwithin its own jurisdiction, You can find testclassloader this class. Then it loads it. It is also said that the Testclassloader class we are running with is not the class we see in Eclipse, but the class in the Testclassloader.jar that we are playing in the Jre\lib\ext directory.

Note: Be sure to put in the JRE directory we currently use , otherwise it will not work, the following steps to confirm, right-click on the project----> Properties--->run/debug settings find their own configuration, Then edit to view the JRE, such as:


  


I will delete the Testclassloader.jar that we hit in the Jre\lib\ext directory, then transform the test class, loop the output class Testclassloader when the name of the class loader, and the name of all the parent ClassLoader. The code is as follows:

Package Com.tgb.clazzloaders;public class Testmain {public static void main (string[] args) throws Exception {//Output class Testcla Ssloader the name of the class loader, and the name of all the parent ClassLoader classloader loader = TestClassLoader.class.getClassLoader (); while (loader! = null) { System.out.println (Loader.getclass (). GetName ()); loader = Loader.getparent ();}}}

The results are as follows:


  


5. Example 2

Write a myservlet that can print out your own class loader and the parent-child structure relationship chain of the current ClassLoader, and after the normal release, see the following print results:


  


Hit the myservlet file into the jar package, put it in the Ext directory, and restart Tomcat. The HttpServlet error was found to be missing. Put the Servlet.jar in the Ext directory as well. The problem is solved, the result of printing is extclassloader. As a result, classes loaded by the parent class loader cannot reference classes that can only be loaded by the quilt-level ClassLoader. such as:


  


6. Summary

The system defaults to three main class loaders (BootStrap, Extclassloader, Appclassloader), each of which is responsible for loading classes at a particular location. All ClassLoader in a Java Virtual machine are organized in a tree structure with a parent-child relationship. The class loader has a certain delegation mechanism. In addition, we can customize our own ClassLoader (inherit ClassLoader), then specify the class loader's jurisdiction (loading directory), and then we can do some special processing of classes (such as encryption) when the class loads.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Java 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.