What is classloading Mechanism in Java
In this tutorial we are going to learn about classloading Mechanism in Java.
The Java Virtual Machine having mainly two main functionalities they are,
- Load the classes into memory (ClassLoading)
- 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.
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.
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
- Bootstrap ClassLoader
- Extensions ClassLoader
- 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.
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.
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.
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.