An Introduction to Java Class Loader

Source: Internet
Author: User

 

An Introduction to Java Class Loader

A
Classloader's basic purpose is to service a request for a class.
JVM needs a class, so it asks the classloader, by name, for this
Class, And the classloader attempts to return a class object that
Represents the class. By overriding different methods corresponding
To different stages of this process, you can create a custom
Classloader.

The
Following is a high-level class-loading algorithm executed by
Classloader when a client requests it to load a class:

  • A
    Check is performed med to see if the requested class has already been
    Loaded by the current classloader. If so, the loaded class is
    Returned and the request is completed. The JVM caches all
    Classes that are loaded by a classloader. A class that has
    Previusly been loaded by a classloader is not loaded
    Again. (classloader # findloadedclass ())

  • If
    The class is not already loaded, the request is delegated to
    Parent classloader, before the current classloader tries to load it.
    This delegation can go all the way up to the bootstrap classloader,
    After which no further delegation is possible.
    (Classloader # findsystemclass ())

  • If
    A parent fails to return a class because it was unable to load it,
    The current classloader will then try to search for the requested
    Class. Each classloader has defined locations where it searches
    Classes to load. For instance, the bootstrap classloader searches in
    The locations (directories and zip/jar files) specified in

    Sun. boot. Class. Path


    System property. The system classloader searches for classes in
    Locations specified by the classpath (set as
    Java. Class. Path


    System property) command-line variable passed in when a JVM starts
    Executing. If the class is found, it's loaded into the system and
    Returned, completing the request.

  • If
    The class is not found, a java. Lang. classnotfoundexception is
    Thrown.

In
Java 1.1, there were no relations between system Class Loader
(Responsible
For loading
In the Java runtime, the application, and classes and resources in
The application's classpath) and Applet Class Loader (loaded classes
Over networks ).

Since
Java 1.2 we have three types of class loaders:

  • Class
    Loaders created automatically by the JVM

  • Program
    Defined class loaders

  • Context
    Class loaders.

There are three class loaders in
First group:

  • Bootstrap
    Class Loader-loads classes from ../JRE/lib/RT. Jar it is the "root"
    In the class loader hierarchy.

  • Extensions
    Class Loader-loads classes from ../JRE/lib/EXT/*. Jar

  • System
    Class Loader-it is responsible for loading in the application,
    Well as for loading classes and resources in the application's
    Classpath.

Second group nodes des:

  • System
    Class Loader-parent class loader by default

  • Additional
    Parent class loader can be specified explicitly

Third group is context class
Loader. A thread's context class loader is, by default, set to
Context class loader of the thread's parent. The hierarchy of threads
Is rooted at the primordial thread (the one that runs the program ).
The context class loader of the primordial thread is set to the class
Loader that loaded the application. So unless you explicitly change
The thread's context class loader, its context class loader will be
The application's class loader. That is, the context class loader can
Load the classes that the application can load.
T
O
Change a thread's context class loader, use
Thread. setcontextclassloader ().

For more information:

The
Basics of Java class loaders


Security
And the class loader Architecture


The
Class Loader Architecture


Understanding
The Java classloader


Understanding
Network class loaders


Internals
Of Java class loading


Java. Lang. classloader


 

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.