Type a search term to find related articles by LIMS subject matter experts gathered from the most trusted and dynamic collaboration tools in the laboratory informatics industry.
The Java class loader, part of the Java Runtime Environment, dynamically loads Java classes into the Java Virtual Machine.[1] Usually classes are only loaded on demand. The virtual machine will only load the class files required for executing the program.[2] The Java run time system does not need to know about files and file systems as this is delegated to the class loader.
A software library is a collection of related object code. In the Java language, libraries are typically packaged in JAR files. Libraries can contain objects of different types. The most important type of object contained in a Jar file is a Java class. A class can be thought of as a named unit of code. The class loader is responsible for locating libraries, reading their contents, and loading the classes contained within the libraries. This loading is typically done "on demand", in that it does not occur until the class is called by the program. A class with a given name can only be loaded once by a given class loader.
Each Java class must be loaded by a class loader.[3][4] Furthermore, Java programs may make use of external libraries (that is, libraries written and provided by someone other than the author of the program) or they may be composed, at least in part, of a number of libraries.
When the JVM is started, three class loaders are used:[5][6][2]
The bootstrap class loader loads the core Java libraries[fn 1] located in the <JAVA_HOME>/jre/lib
(or <JAVA_HOME>/jmods>
for Java 9 and above) directory. This class loader, which is part of the core JVM, is written in native code. The bootstrap class loader is not associated with any ClassLoader
object.[2] For instance, StringBuilder.class.getClassLoader()
returns null
.[2]
The extensions class loader loads the code in the extensions directories (<JAVA_HOME>/jre/lib/ext
,[5] or any other directory specified
by the java.ext.dirs
system property).
The system class loader loads code found on java.class.path
, which maps to the CLASSPATH
environment variable.
The Java class loader is written in Java. It is therefore possible to create a custom class loader without understanding the finer details of the Java Virtual Machine. Apart from the Bootstrap class loader, every Java class loader has a parent class loader.[7] The parent class loader is defined when a new class loader is instantiated or set to the virtual machine's system default class loader.
This makes it possible (for example):
Jakarta EE (formerly Java EE and J2EE) application servers typically load classes from a deployed WAR or EAR archive by a tree of class loaders, isolating the application from other applications, but sharing classes between deployed modules. So-called "servlet containers" are typically implemented in terms of multiple class loaders.[4][9]
JAR hell is a term similar to DLL hell used to describe all the various ways in which the classloading process can end up not working.[10] Three ways JAR hell can occur are:
The OSGi Alliance specified (starting as JSR 8 in 1998) a modularity framework that aims to solve JAR hell for current and future VMs in ME, SE, and EE that is widely adopted. Using metadata in the JAR manifest, JAR files (called bundles) are wired on a per-package basis. Bundles can export packages, import packages and keep packages private, providing the basic constructs of modularity and versioned dependency management.
Java 9 introduced the Java Platform Module System in 2017. This specifies a distribution format for collections of Java code and associated resources. It also specifies a repository for storing these collections, or modules, and identifies how they can be discovered, loaded and checked for integrity. It includes features such as namespaces with the aim of fixing some of the shortcomings in the existing JAR format. The Java Platform Module System follows a different philosophy from the OSGi architecture that aims at providing modularity for the Java Runtime Environment in a backwards-compatible way that uses the default mechanism of loading classes that the JRE provides. However, since the Java Platform Module System does not offer the ability for controlled co-existence of libraries with different versions, it does not fully address the JAR hell problem.[12]