Java ClassLoader Loading mechanism

Source: Internet
Author: User

I. Architecture (from top to bottom)

1.Bootstrap ClassLoader (Bootstrapclassloader)---Start the ClassLoader or boot class loader, loading the APIs of the JDK core, which are typically located under Jdk_home/lib; it is a local interface, So it's not possible to get its information from Java code. For example, log (Java.lang.String.class.getClassLoader ()) gets null.

2.Extension ClassLoader (launcher$extclassloader)---Extension class loader, responsible for loading the API under the Jdk_home/lib/ext directory;

3.System ClassLoader (launcher$appclassloader)---system ClassLoader is primarily responsible for APIs in the directory referred to by Java-classpath.

4.Custom ClassLoader---User-defined class loader, which is a subclass of Java.lang.ClassLoader), is responsible for dynamically loading class files through Java.lang.ClassLoader subclasses while the program is running.

Two. Loading mechanism

Java's ClassLoader is presented in a parent-child structure. When a request is made to the loader to request a class, the first step is to find out if the required class has already been loaded, if it exists, to return the class, if it does not exist, to delegate the current loader's parent loader to complete the load class request, if the parent loader has a previous level, the upper level of the record request to load the required class If the top-level loader (Bootstrapclassloader) fails to load the class, it goes back to the next level, and if the next level still fails to load, then the next level, until the current ClassLoader, throws Classnotfoundexceptio if it is still not loaded . Note: The parent-child relationship here is not an inheritance relationship in Java and can be understood as a delegate relationship.

Three. Unique identification of the class

In the JVM, a class's unique identity is (class name, package name, loader), and classes loaded by different loaders, even with the same class name and package name, are not considered to be the same instance, not type-comparable. For example: ClassLoader A has two sub-loaders ClassLoader B and ClassLoader C, and class Foo is visible to Classloaderb and Classloaderc, which is invisible to Classloadera, When a request is made to Classloaderb and Classloaderc for an instance of Foo, because the two loaders load foo Separately, the JVM generates two instances of Foo (FOO1,FOO2), and they are not comparable, and the statement foo1= (Foo) Foo2 will throw classcastexception, of course, if Foo is visible to Classloadera, Foo will be loaded by Classloadera and will not produce classcastexception.

Four. Servelet Container class Loader

A simple test in TOMAT5 found that Thread.CurrentThread (). Getcontextclassloader () Gets the following result:

WebApp Classloader:webappclassloader
Delegate:false
Repositories:
/web-inf/classes/
----------> Parent Classloader:
[Email protected]

According to the phrase "Servlet specification, version 2.3, section 9.7.2 WEB application Classloader", you should be able to load resources from any library file through WebappClassLoader , but cannot overwrite j2se and servlet APIs; the servlet loader does not allow the servlet to access the implementation class of the Web container in the future.

Here, excerpt the Order of the TOMCAT5 and TOMCAT6 classes loaded on the official web:

Tomcat 5:

Bootstrap classes of your JVM
System class Loader classes
/web-inf/classes of your WEB application
/web-inf/lib/*.jar of your WEB application
$CATALINA _home/common/classes
$CATALINA _home/common/endorsed/*.jar
$CATALINA _home/common/i18n/*.jar
$CATALINA _home/common/lib/*.jar
$CATALINA _base/shared/classes
$CATALINA _base/shared/lib/*.jar

Tomcat 6:

Bootstrap classes of your JVM
System class Loader classes
/web-inf/classes of your WEB application
/web-inf/lib/*.jar of your WEB application
$CATALINA _home/lib
$CATALINA _home/lib/*.jar

Java classloader loading mechanism

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.