Java ClassLoader Basics

Source: Internet
Author: User

Java has a total of four classloader, the reason is called Class loader, is the program to use a class, the class loader to load memory.
These four classloader are:Bootstrap ClassLoader,Extension ClassLoader,appclassloader
and URLClassLoader, their role in fact from the name can presumably be inferred. Where Appclassloader is called System ClassLoader in many places

Bootstrap ClassLoader is the core class that loads Java when the JVM starts to run and is written in C + +, which is used to load the core class library, which is written in the JVM source code:
static const char classpathformat[] =
"%/lib/rt.jar:"
"%/lib/i18n.jar:"
"%/lib/sunrsasign.jar:"
"%/lib/jsse.jar:"
"%/lib/jce.jar:"
"%/lib/charsets.jar:"
"%/classes";
Extension ClassLoader is used to load the extension class, which is the class in /lib/ext .
The class that Appclassloader uses to load Classpath is the class that is most closely related to us.
The urlclassloader is used to load the remote classes on the network and is not discussed for the moment.


the relationship between them:

1.Parent-child, in order from large to small. is not a simple inheritance relationship.

2.ClassLoader has agetParentmethod, but the Ext ClassLoader call Gets theNULL, Bootstrap is the JVM's own, and the user cannot see it.

3.classloader ofdelegation Mechanism: When a class is loaded by a lower-level classloader, it first requests the parent loader to load, and the parent requests its parent
For example, now ext is going to load, it requests. If the largest bootstrap can not find, then boot will be called Ext himself to find, ext can't find, iswon'tLet the next level of the app to find, at this time to quote ClassNotFoundException

4. Class A call class B,b will require the class loader to invoke its class to load it, that is, B will require loading A's loader to load B. There's a problem, if they're together, that's okay, and certain classloader will load both of them. But what if a is in the/lib/ext folder and B is in classpath? The process is such that first load a, then a layer to bootstrap classloader,boot did not find so ext himself find, found, no problem; load B, because a calls B, so also from Bootstrap to find, not found, then A's ext ClassLoader to something else is not found, but no longer call down, so reported ClassNotFoundException.
But in real life there are many applications, such as the JDBC core method in the core library and drive in the expansion of the library, is bound to two places, then how to do? To use the context ClassLoader we can specify a suitable classloader as the thread's context for this thread by using the Setcontextclassloader method when creating thread threads. ClassLoader, when this thread is running, we can use the Getcontextclassloader method to get this context ClassLoader, so we can load it into the class we need. The default is System ClassLoader. With this feature, we can "break" the ClassLoader delegation mechanism, and the parent ClassLoader can get the context ClassLoader of the current thread, and this context ClassLoader can be its child classloader or other ClassLoader, then the parent ClassLoader can obtain the desired Class from it, which breaks the limit that can only be requested by the parent ClassLoader. This mechanism can satisfy when our classpath is determined at runtime and loaded by a custom ClassLoader, the class loaded by the system ClassLoader (that is, in the JVM classpath) can be passed through the context ClassLoader gets custom ClassLoader and loads into a specific class (usually abstract classes and interfaces, which are implemented in custom ClassLoader), such as a servlet in a Web application that is loaded with this mechanism.

Java ClassLoader Basics

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.