Skip to main content

What is ClassLoader in Java ?

The Java ClassLoader is a part of the Java Runtime Environment (JRE) that dynamically loads Java classes into the Java Virtual Machine (JVM) during runtime.

The JVM doesn't need to know about the underlying files or file systems in order to run Java programs thanks to class loaders.

Moreover, these Java classes aren't loaded into memory all at once, but rather when they're required by an application.

Not all classes are loaded by a single class loader, but there are three different class loaders to do the job.

1) BootStrap ClassLoader

When a JVM starts up, a special chunk of platform-specific machine code runs that loads the classloader subsystem and kicks off the whole classloading process.

This machine code is known as the Bootstrap/Primordial (or sometimes - Null) classloader.

It is not a Java class at all, as are all other classloaders.

Loading the first pure Java classloader is the job of the bootstrap classloader.

The bootstrap classloader also takes care of loading all of the code needed to support the basic Java Runtime Environment (JRE), including classes in the rt.jar, java.util and java.lang packages.

Bootstrap ClassLoader doesn't have any parent ClassLoaders.

2) Extension ClassLoader

The Extension ClassLoader is the child classloader of Bootstrap and the parent classloader of System classloader. It loads the extensions of core Java classes located inside $JAVA_HOME/jre/lib/ext directory or any other directory pointed to by the system property java.ext.dirs.

3) System ClassLoader

System ClassLoader is also called Application ClassLoader and it loads the classes found in the environment variable CLASSPATH, -classpath or -cp command line option. You can change the classpath using the "-cp" or "-classpath" switch.

How ClassLoader sub-system work in Java ?

Delegation

Class loaders follow the delegation model, where on request to find a class or resource, a ClassLoader instance will delegate the search of the class or resource to the parent class loader.

Let's say we have a request to load an application class into the JVM. The application class loader first delegates the loading of that class to its parent extension class loader, which in turn delegates it to the bootstrap class loader.

Only if the bootstrap and then the extension class loader are unsuccessful in loading the class, does the system class loader try to load the class itself.


If the last system class loader isn't able to load the class either, it throws java.lang.NoClassDefFoundError or java.lang.ClassNotFoundException.

Visibility

A class loaded by a parent ClassLoader is visible to the child ClassLoaders but a class loaded by a child ClassLoader is not visible to the parent ClassLoaders.

Uniqueness

As a consequence of the delegation model, it's easy to ensure unique classes, as we always try to delegate upwards.

If the parent class loader isn't able to find the class, only then will the current instance attempt to do so itself.

Custom ClassLoader

The built-in class loader is sufficient for most cases where the files are already in the file system.

However, in scenarios where we need to load classes out of the local hard drive or a network, we may need to make use of custom class loaders.


Comments

Popular posts from this blog

Deploying Spring Boot microservices on Kubernetes Cluster

This article guides you through the deployment of two Spring Boot microservices, namely "order-service" and "inventory-service," on Kubernetes using "MiniKube" . We will establish communication between them, with "order-service" making calls to an endpoint in "inventory-service." Additionally, we will configure "order-service" to be accessible from the local machine's browser . 1) Create Spring Boot microservices The Spring Boot microservices, "order-service" and "inventory-service," have been developed and can be found in this GitHub repository. If you are interested in learning more about creating Spring Boot REST microservices, please refer to this or this (Reactive) link. 2) Build Docker Images The Docker images for both "order-service" and "inventory-service" have already been generated and deployed on DockerHub, as shown below. codeburps/order-service cod...

Circuit Breaker Pattern with Resilience4J in a Spring Boot Application

Read Also: Spring Cloud Circuit Breaker + Resilience4j Resilience4j is a lightweight fault tolerance library that draws inspiration from Netflix Hystrix but is specifically crafted for functional programming. The library offers higher-order functions, known as decorators , designed to augment any functional interface, lambda expression, or method reference with features such as Circuit Breaker, Rate Limiter, Retry, or Bulkhead . These functionalities can be seamlessly integrated within a project, class, or even applied to a single method. It's possible to layer multiple decorators on any functional interface, lambda expression, or method reference, allowing for versatile and customizable fault tolerance. While numerous annotation-based implementations exist online, this article focuses solely on the reactive approach using router predicates and router functions . How Circuit Breaker Pattern works? In general, a circuit breaker functions as an automatic electrical s...

Declarative REST Client in Spring Boot (Spring 6 HTTP Interface)

Feign , an early declarative REST client, was initially part of the Spring Cloud Netflix stack and later rebranded as Spring Cloud OpenFeign . Before its introduction, crafting HTTP calls using RestTemplate involved repetitive code for each service interaction. With Feign, developers could simply define an interface with method contracts mirroring the service's endpoints. Behind the scenes, proxy magic generated a fully functional HTTP client, eliminating the need for boilerplate code . HTTP Interface (Spring Framework 6) The recent release of Spring Framework 6 integrated this declarative REST client as a native part of the core web framework in the form of the HTTP Interface . All the necessary components reside in the spring-web module, which is a transitive dependency for either the spring-boot-starter-web or spring-boot-starter-webflux modules. Currently, the WebFlux dependency is essential due to the HttpServiceProxyFactory , responsible for client gener...