• Java Class loader


     

     

    Introduction : What is Classloader

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

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


    How Classloader works

    An application might require different classes to function properly at runtime, Whenever an application requires a class at runtime, ClassLoader will look for the given class name and if found it will load it in memory.  Once provided the name of class to load, ClassLoader will try to locate data that constitue  the definition of class at different locations.

    Location searched by the ClassLoader will 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 extension directory.
    2. Second look for the class in classpath(java.class.path property). Default value of the classpath is current directory but this value can be changed in different way.

    ClassLoader Hierarchy

    ClassLoader uses a delegation model to search for classes and resources to load. Each instance of ClassLoader will have an associated parent class loader. When a classloader get request to find a class, given ClassLoader will delegate the search for the class or resource to its 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 have any parent.


    Type of Classloader

    Java ClassLoader can be divided in 4 types.

    Bootstrap Class Loader

    It is the Super Class Loader. It doesn’t have any parent ClassLoader. It loads the Core classes of Java like class in java.lang, java.util package.

    Extension Class Loader

    Extension ClassLoader will load the classes in JAR files in the lib/ext directory of the JRE, and in the system-wide, platform-specific extension directory. Bootstrap ClassLoader will 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 will load the classes in classpath(java.class.path property). Extension ClassLoader works as the parent of System ClassLoader. Default value of the classpath is current directory but this value can be changed in different way.

    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.
  • 相关阅读:
    现代软件工程_团队项目_阿尔法阶段_阿里云服务器部署_2017.11.24
    现代软件工程_团队项目_阿尔法阶段_前端知识共享_2017.11.21
    现代软件工程_团队项目_阿尔法阶段_第四次会议记录_2017.11.20
    现代软件工程_团队项目_阿尔法阶段_第三次会议记录_2017.11.15
    现代软件工程_团队项目_阿尔法阶段_前端页面原型v1.0.2_2017.11.14
    现代软件工程_团队项目_阿尔法阶段_需求分析文档_2017.11.13
    现代软件工程_团队项目_阿尔法阶段_第二次会议记录_2017.11.13
    现代软件工程_团队项目_阿尔法阶段_团队展示_2017.11.12
    现代软件工程_团队项目_阿尔法阶段_市场调研问卷_2017.11.11
    c++ 内存分配 虚函数实现多态等
  • 原文地址:https://www.cnblogs.com/hephec/p/4556677.html
Copyright © 2020-2023  润新知