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.
Cite error: There are <ref group=fn>
tags on this page, but the references will not show without a {{reflist|group=fn}}
template (see the help page).