Java Classloading Mechanism - BEHIND JAVA

Java Classloading Mechanism

Share This

The Java Virtual Machine having mainly two main functionalities they are,
1. Load the classes into memory (ClassLoading)
2. Execution of code.

Here we can discuss the first functionality of JVM - ClassLoading. The ClassLoader is used to load the Java classes into the Java runtime environment. So the JVM doesn't know anything about the class that will be loaded at runtime, those are done by Classloaders. The EJB Containers and Servlet Containers (Tomcat, JBoss ...etc) are implemented there own custom ClassLoaders to support the features like hot deployment and runtime platform extensibility.

ClassLoader Working

Let's move on the working of ClassLoader. We know that the input of the Java Runtime Environment is .class files(or the compiled java files) which contains the binary data of your corresponding java class. The ClassLoader first loads the main class file (a class file which contains the main method) and it reads the Bytecode inside of that and creates the java.lang.Class object. In ClassLoader, the main class will be loaded first and other class and interface are loaded as they get referenced in the bytecode being executed. So it means that the classloader being load the classes only when it required and it is known as lazy loading.

ClassLoading Delegation Model

It is also possible to initialize multiple ClassLoaders in JVM. The ClassLoaders are arranged in a hierarchical manner. If there is a request that applied to a classloader to load the given class first it will delegate the request to its parent classloader then only after the requested class loader tries to load the class itself. The parent class loader also delegate the request to its parent and this chain of delegation continues to the bootstrap classloader. If a class loaders parent can load a given class, it returns that class. Otherwise, the class loader attempts to load the class itself.

The JVM Contains three classloaders which are
1. Bootstrap ClassLoader
2. Extensions ClassLoader
3. Application ClassLoader

Bootstrap ClassLoader: - At the top of the hierarchy is the bootstrap class loader. This ClassLoader is responsible for loading only the classes that are from the core Java™ API. These classes are the most trusted and are used to bootstrap the JVM.

Extensions ClassLoader: - The extensions ClassLoader can load classes that are standard extensions packages in the extensions directory (the ext directory under JRE installed directory).

Application ClassLoader: - The application class loader can load classes from the local file system, and will load files from the CLASSPATH. The application class loader is the parent of any custom class loader or hierarchy of custom class loaders.

Diverging from Java2 Deligation Model

The above delegation model is not suited for all situations. The main example of it is Web Application ClassLoader. The Web Application ClassLoader be implemented so that the classes and resources packaged in the Web application archive are loaded in preference to classes and resources residing in container-wide jar files. To meet this recommendation, a Web application ClassLoader should search for classes and resources in its local repository first before delegating to a parent class loader So it deviating from a normal class loading delegation model. Let's meet the tomcat classloader WebappX

WebappX: - In WebappX When a request to load a class from the web application's WebappX class loader is processed, this class loader will look in the local repositories first, instead of delegating before looking. There are exceptions. Classes which are part of the JRE base classes cannot be overridden. Also, any JAR file that contains Servlet API classes will be explicitly ignored by the classloader — Do not include such JARs in your web application. All other class loaders in Tomcat 6 follow the usual delegation pattern.

Applications of ClassLoaders

1. Hot Deployment.

Upgrading software in a running application without restarting it is known as hot deployment. For a Java application, hot deployment means upgrading Java classes at runtime. ClassLoaders play an important role in Java-based application servers to achieve hot deployment.

2. Modifying the Class Files

A ClassLoader searches for bytecodes of a class file in the findClass method. After the bytecodes have been located and read into the program, they may be modified before invoking defineClass.

12 comments:

  1. Really wonderful information for this one,i clearly understand all given information.Thanks admin for sharing the unique content, you have done a great job I appreciate your effort.
    informatica

    ReplyDelete
  2. Nice blog! Sometimes I use your hints in my work. Have you ever read smth about virtual data rooms ? it is a cloud service for business. I'm more than sure that this will be interesting fro you.

    ReplyDelete
  3. This is very good content you share on this blog. it's very informative and provide me future related information.
    python training in chennai
    python training in Bangalore

    ReplyDelete
  4. Nice tutorial. Thanks for sharing the valuable information. it’s really helpful. Who want to learn this blog most helpful. Keep sharing on updated tutorials…
    java training in tambaram | java training in velachery

    java training in omr | oracle training in chennai

    ReplyDelete
  5. This is such a good post. One of the best posts that I\'ve read in my whole life. I am so happy that you chose this day to give me this. Please, continue to give me such valuable posts. Cheers!

    Data Science course in Chennai
    Data science course in bangalore
    Data science course in pune
    Data science online course

    ReplyDelete
  6. very nice blogs!!! i have to learning for lot of information for this sites...Sharing for wonderful information.Thanks for sharing this valuable information to our vision.keep up it!!

    Android Training in Chennai

    Android Online Training in Chennai

    Android Training in Bangalore

    Android Training in Hyderabad

    Android Training in Coimbatore

    Android Training

    Android Online Training



    ReplyDelete
  7. This is such a good post. One of the best posts that I\'ve read in my whole life. I am so happy that you chose this day to give me this. Please, continue to give me such valuable posts. Cheers!
    hardware and networking training in chennai

    hardware and networking training in omr

    xamarin training in chennai

    xamarin training in omr

    ios training in chennai

    ios training in omr

    iot training in chennai

    iot training in omr

    ReplyDelete

Pages