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.

Next
This is the most recent post.
Previous
Older Post

6 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. Great Article I love to read your articles because your writing style is too good, its is very very helpful for all of us and I never get bored while reading your article because it becomes more and more interesting from the starting lines until the end. So Thank you for sharing a COOL Meaningful stuff with us Keep it up..!

    SAP training in Chennai

    ReplyDelete

  3. I’m sincerely suggesting your blog to all my friends… I’ve changed myself in many thing after reading your blog… Thanks and keep going.

    Digital marketing company in Chennai

    ReplyDelete
  4. Hello Everybody!
    Can you give me some minutes for introduct my service? I'm providingmat ong rung nguyen chat | Special is gia mat ong rung | and mat ong rung | and mat ong nguyen chat | and mat ong rung nguyen chat gia re | and mat ong | and mat ong rung thien nhien. Contact with me.
    Thanks for reading! And have a good day!

    ReplyDelete
  5. 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

 
Top
Blogger Template