The ClassLoader is a way of customizing the way Java gets its classes and loads them into memory. The verifier and other standard Java things still run, but the ClassLoader is allowed great flexibility in determining where to get the classfiles and when to load and resolve them. For that matter, a custom ClassLoader can perform on-the-fly code generation or modification!
Every classloader has a parent classloader that is consulted before the 'child' classloader when classes or resources should be loaded. This is done to make sure that classes can be loaded from an hierarchy of multiple classloaders and classloaders do not accidentially redefine already loaded classes by classloaders higher in the hierarchy.
The grandparent of all classloaders is the bootstrap classloader, which loads all the standard system classes as implemented by GNU Classpath. The other special classloader is the system classloader (also called application classloader) that loads all classes from the CLASSPATH (java.class.path
system property). The system classloader is responsible for finding the application classes from the classpath, and delegates all requests for the standard library classes to its parent the bootstrap classloader. Most programs will load all their classes through the system classloaders.
The bootstrap classloader in GNU Classpath is implemented as a couple of static (native) methods on the package private class java.lang.VMClassLoader
, the system classloader is an anonymous inner class of ClassLoader and a subclass of java.net.URLClassLoader
.
Users of a ClassLoader
will normally just use the methods
-
loadClass()
to load a class. -
getResource()
or getResourceAsStream()
to access a resource. -
getResources()
to get an Enumeration of URLs to all the resources provided by the classloader and its parents with the same name.
Subclasses should implement the methods
-
findClass()
which is called by loadClass()
when the parent classloader cannot provide a named class. -
findResource()
which is called by getResource()
when the parent classloader cannot provide a named resource. -
findResources()
which is called by getResource()
to combine all the resources with the same name from the classloader and its parents. -
findLibrary()
which is called by Runtime.loadLibrary()
when a class defined by the classloader wants to load a native library.
@author John Keiser
@author Mark Wielaard
@author Eric Blake (ebb9@email.byu.edu)
@see Class
@since 1.0
@status still missing 1.4 functionality