Java class loader and parent class delegation mechanism

Source: Internet
Author: User

1. Class Loader

Multiple ClassLoader can be installed in a Java virtual machine, and the system defaults to the main three classloader, each of which is responsible for loading the class at a particular location: BootStrap (embedded in the Java virtual machine, written in C + +), Extclassloader,appclassloader. Of course, you can also customize the ClassLoader, and the custom loader must inherit ClassLoader.

All ClassLoader in a Java Virtual machine are organized in a tree structure with a parent-child relationship, and when each ClassLoader object is instantiated, it is necessary to specify a parent class loader object for it or to load the parent class by default with the System class loader.

2. Let's take a look at the following code:

public class Classloadertest {public static void main (string[] args) {ClassLoader loader = ClassLoaderTest.class.getClass Loader (); while (Loader! = null) {System.out.println (Loader.getclass (). GetName ()); Loader = Loader.getparent ();} SYSTEM.OUT.PRINTLN (loader);}}

The output of the successively:

Sun.misc.launcher$appclassloader

Sun.misc.launcher$extclassloader

NULL: NOTE NULL is represented as the bootstrap ClassLoader, which is the top loader, without the parent ClassLoader


3. Parent-child relationships and jurisdictions between class loaders:

BootStrap, Extclassloader, Appclassloader (commonly referred to as System ClassLoader)

Their scope of jurisdiction is:
BootStrap------>jre/lib/rt.jar
Extclassloader---------->jre/lib/ext/*.jar
Appclassloader----------All jars or directories specified by the >classpath.

4. Parent class delegation mechanism

The parent class delegate mechanism should have its own parent class loader in addition to the top-level startup class loader. The parent-child relationship between ClassLoader is generally not implemented as an inherited relationship, but instead uses the code of the parent loader with the combined relationship.

The parent class delegation mechanism works by: If a class loader receives a request for a class load, he first does not attempt to load the class himself, but instead delegates the request to the parent class loader to complete, so that all load requests should eventually be routed to the top-level startup ClassLoader. The child loader tries to load itself only when the parent loader has feedback that it cannot complete the load request.

The following is the class loader level display:

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M01/78/59/wKiom1Z6YjnhpemRAAA-oWQP0N8760.png "title=" Drawing 1.png "alt=" Wkiom1z6yjnhpemraaa-owqp0n8760.png "/>


Java class loader and parent class delegation mechanism

Related Article

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.