Java is the most famous language in the programming world. One of the key features of Java is its use of classloaders, which are responsible for loading classes and resources into the JVM. Java has various types of classloaders, each with its own arrangement of obligations and capacities.
Table of Contents
7 Types of ClassLoaders In Java
In this article, we’ll investigate the different types of classloaders in Java and how they work. They are mainly of 7 types, listed below:
Bootstrap ClassLoader
The Bootstrap ClassLoader is the first classloader to be loaded when a JVM starts. It loads core Java classes such as Java.lang and java.util. These classes are part of the Java runtime environment and are always available to all applications.
The Bootstrap Classloader is highly privileged and is trusted by the JVM to provide the necessary classes and resources for the execution of Java programs. It loads classes from the default bootstrap classpath and is responsible for loading the Java runtime, including the classes in the rt.jar file.
Extension ClassLoader
The Extension ClassLoader is liable for stacking classes from the Java augmentation registry. This directory is typically in the JDK installation directory and contains additional libraries and classes, not part of the core Java runtime environment.
When a Java application or applet requests a class that is unavailable in the application’s classpath, the JVM first delegates the task of finding and loading the class to the extension classloader.
If the class is found in the extensions directory, the extension classloader loads it into the JVM. If not, the task is delegated to the next classloader in the hierarchy, typically the system classloader.
System ClassLoader
The System ClassLoader is answerable for stacking classes from the application classpath. This includes classes from the application’s class files and any third-party libraries included in the classpath.
The System Classloader is located between the Bootstrap Classloader and the Extension Classloader in the Java classloading hierarchy. The Bootstrap Classloader loads the core Java classes in the Java runtime environment. At the same time, the Extension Classloader is responsible for loading classes and resources that are part of Java extensions.
User-Defined ClassLoader
In addition to the built-in classloaders, Java allows for creating custom classloaders. A User-Defined ClassLoader can load classes from non-standard locations, such as a database or a remote server.
Parallel ClassLoader
Starting with Java 7, the JVM introduced a new parallel classloading mechanism. This allows the JVM to load classes in parallel, which can improve application startup times on multi-core systems. Java 7 and later versions use the parallel classloader by default.
The Parallel Classloader has typical usage in high-performance applications that require fast class loading times, such as web servers or application servers. It also commonly finds its usage in large-scale distributed systems where many nodes need to load classes in parallel.
Application ClassLoader
The Application ClassLoader is a type of classloader that can load classes that are part of the application. This includes the application’s class files and any third-party libraries included in the classpath. The Application classloader is a subclass of the Framework classloader.
The Application ClassLoader has a hierarchical structure, where it delegates to the Extension ClassLoader and the Bootstrap ClassLoader, which load classes from the extension directories and the system classes, respectively.
Suppose a class is not available in the Application ClassLoader. In that case, it delegates the loading of the class to the Extension ClassLoader and then to the Bootstrap ClassLoader, until it finds the class or an error occurs.
Thread Context ClassLoader
The current thread uses Thread Context ClassLoader, a special classloader, to load classes and resources. This can be useful when a thread needs to load classes not available on the application classpath.
Conclusion
In conclusion, classloaders are a critical component of the Java runtime environment. They are responsible for loading classes and resources into the JVM, and each type of classloader has its responsibilities and capabilities. Understanding the different kinds of classloaders in Java can help developers write more efficient and flexible applications.
See also: Java int to byte array
Hi, I’m Geoff. I design. I develop. I do lots of things in between. What that really boils down to is that I make websites