Java Class Loader

Source: Internet
Author: User

Introduction:what is Classloader

All classes files in application is not loaded to memory at startup, but is loaded on demand as needed by the program, T His loading of class are done by ClassLoader. ClassLoader is a part of the JVM that loads classes into memory.

The Java ClassLoader is written in Java itself (opposite to JVM which are written in C + +). Which means that it's easy-to-develope custom Class Loader when ever required without have to understand the details of The JVM. ClassLoader is an abstract class which are the part of the Java.lang package.

How Classloader Works

An application might require different classes to function properly at runtime, whenever an application requires a Class A  T runtime, ClassLoader'll look for the given class name and if found it'll load it in memory. Once provided the name of class to load, ClassLoader would try to locate data that constitue the definition of class at di Fferent locations.

Location searched by the ClassLoader would be in given order.

    1. First look for the classes in JAR files in the Lib/ext directory of the JRE, and in the System-wide, platform-specific ext Ension directory.
    2. Second look for the class in Classpath (Java.class.path property). Default value of the classpath is current directory and this value can be changed in different.

ClassLoader Hierarchy

ClassLoader uses a delegation model to search for classes and resources to load. Each instance of ClassLoader'll has an associated parent class loader. When a ClassLoader GET request to find a class, given ClassLoader would delegate the search for the class or Resou Rce to it parent class loader before attempting to find the class or resource itself. Bootstrap class Loader is on the top of class Loader hierarchy. Bootstrap ClassLoader is the built in class loader in virtual machine. Bootstrap class loader doesn ' t has any parent.

Type of Classloader

Java ClassLoader can divided in 4 types.

Bootstrap Class Loader

It is the Super Class Loader. It doesn ' t has any parent ClassLoader. It loads the Core classes of Java like class in Java.lang, Java.util.

Extension Class Loader

Extension ClassLoader would load the classes in JAR files in the Lib/ext directory of the JRE, and in the System-wide, plat Form-specific extension directory. Bootstrap ClassLoader would be the parent of Extension ClassLoader. Developer can manually add jars in ext folder to get it loaded by Extension ClassLoader.

System Class Loader

System ClassLoader would load the classes in Classpath (java.class.pathproperty). Extension ClassLoader works as the parent of System ClassLoader. Default value of the classpath is current directory and this value can be changed in different.

    1. Setting the CLASSPATH enviornment variable.
    2. Value providing by-classpath OR-CP command-line options.
Custom Class Loader

Develoeper can also create Custom class according to there need.

Advantage of Using Custom ClassLoader
    1. Hotdeployment in Application Server
    2. Custom ClassLoader in case of browser to check for security certification before executing untrusted code.

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.