About
A class loader is a piece of code that load a class.
Class loaders may typically be used by security managers to indicate security domains.
Articles Related
Process
Classes are loaded into the JVM only on demand, so even though classes might be available for the bootstrap class loader, the application needs to access them to trigger their actual loading.
Bootstrap
The bootstrap class loader is the first class loader. It
- is written in native platform-dependent code
- loads the system classes to run the JVM (java.lang package, classes for Java primitives, and so forth)
A developer can expand the set of classes that the bootstrap class loader will be able to load by using the -Xbootclasspath JVM option
(JVM|user-defined)
Application classes are loaded using an instance of the java.lang.Classloader. The JVM provides already one default implementation.
Example:
public class A {
public static void main(String[] args) {
B b = new B();
int i = b.inc(0);
System.out.println(i);
}
}
Only when the JVM encounters the bytecode instructions for the
new B()statement will it try to locate and load class B. Every already loaded class contains a reference to its class loader. This class loader is used to load all the classes referenced from that class. class B will be loaded with the following piece of code.
A.class.getClassLoader().loadClass("B")
Every class object contains a reference to the ClassLoader that defined it. See getClassLoader method
Hierarchy
The class loaders in the JVM are organized into a tree hierarchy, in which every class loader has a parent. The root class loader of the hierarchy is the bootstrap class loader.
Normally, prior to trying to locate and load a class, a class loader will check whether the class’s parent in the hierarchy can load—or already has loaded—the required class.
The two next class loader in the hierarchy are:
- the extension loader
- and the system loader
Both are of the URLClassloader type.
Extension
The extension class loader loads the classes from the extension directories.
The locations of the extension directories are specified via the java.ext.dirs system property.
System
The system class loader loads the application classes and the classes available on the classpath.
Class Conflict
The conflict resolution strategy is simple and straightforward: the first appropriate class wins.
Relying on the order of directories in the classpath is a fragile practice, so instead the developer can add the classes to -Xbootclasspath to ensure that they will be loaded first.
Resolution, Linking, and Verification
After a class is located and its initial in-memory representation created in the JVM process, it is:
- verified,
- prepared,
- resolved,
- and initialized
Implementation
A class loader must implement the following interface ClassLoader.
JVM
The Java virtual machine has its own built-in implementation called the “bootstrap class loader”.
When Java classes are loaded by the Java Virtual Machine, it searches in a platform-dependent manner different file system directories specified in the CLASSPATH variable to resolve references to the classes needed for execution.
Application
An application can write its own specific class loader that will attempt to:
- locate
- or generate data that constitutes a definition for the class. (writing the code into a class file on a file system and then loading it)
Example
For example, an application could create a network class loader to download class files from a server. Sample code might look like:
ClassLoader loader = new NetworkClassLoader(host, port);
Object main = loader.loadClass("Main", true).newInstance();
// Instances of class can be created using Class.newInstance
. . .
The network class loader subclass must follow the following steps:
- define the loadClassData method in order to download the bytes that make up the class from the network
- use the method defineClass to convert this bytes and create a class instance. (The method defineClass converts an array of bytes into an instance of class Class).
- define the methods findClass that stitch the two previous method in order to return the class.
A sample implementation is:
class NetworkClassLoader extends ClassLoader {
String host;
int port;
public Class findClass(String name) {
byte[] b = loadClassData(name);
return defineClass(name, b, 0, b.length);
}
private byte[] loadClassData(String name) {
// load the class data from the connection
. . .
}
}
Class Reference (Dependency|Resolution)
The methods and constructors of objects created by a class loader may reference other classes. To determine the class(es) referred to, the Java virtual machine invokes the loadClass method of the class loader that originally created the class.
Documentation / Reference
- AntClassLoader