Top Description Inners Fields Constructors Methods
java.lang

public abstract Class ClassLoader

Additional top-level class in compilation unit: CompoundEnumeration.

extends Object
Class Inheritance
Known Direct Subclasses
java.security.SecureClassLoader, jdk.internal.reflect.DelegatingClassLoader
Imports
java.io.InputStream, .IOException, .UncheckedIOException, .File, java.lang.reflect.Constructor, .InvocationTargetException, java.net.URL, java.security.AccessController, .AccessControlContext, .CodeSource, .PrivilegedAction, .ProtectionDomain, java.security.cert.Certificate, java.util.ArrayList, .Collections, .Enumeration, .HashMap, .Map, .NoSuchElementException, .Objects, .Set, .Spliterator, .Spliterators, .WeakHashMap, java.util.concurrent.ConcurrentHashMap, java.util.function.Supplier, java.util.stream.Stream, .StreamSupport, jdk.internal.loader.BootLoader, .BuiltinClassLoader, .ClassLoaders, .NativeLibrary, .NativeLibraries, jdk.internal.perf.PerfCounter, jdk.internal.misc.Unsafe, .VM, jdk.internal.reflect.CallerSensitive, .CallerSensitiveAdapter, .Reflection, jdk.internal.util.StaticProperty, sun.reflect.misc.ReflectUtil, sun.security.util.SecurityConstants

A class loader is an object that is responsible for loading classes. The class ClassLoader is an abstract class. Given the binary name of a class, a class loader should attempt to locate or generate data that constitutes a definition for the class. A typical strategy is to transform the name into a file name and then read a "class file" of that name from a file system.

Every Class object contains a reference to the ClassLoader that defined it.

Class objects for array classes are not created by class loaders, but are created automatically as required by the Java runtime. The class loader for an array class, as returned by Class#getClassLoader() is the same as the class loader for its element type; if the element type is a primitive type, then the array class has no class loader.

Applications implement subclasses of ClassLoader in order to extend the manner in which the Java virtual machine dynamically loads classes.

Class loaders may typically be used by security managers to indicate security domains.

In addition to loading classes, a class loader is also responsible for locating resources. A resource is some data (a ".class" file, configuration data, or an image for example) that is identified with an abstract '/'-separated path name. Resources are typically packaged with an application or library so that they can be located by code in the application or library. In some cases, the resources are included so that they can be located by other libraries.

The ClassLoader class uses a delegation model to search for classes and resources. Each instance of ClassLoader has an associated parent class loader. When requested to find a class or resource, a ClassLoader instance will usually delegate the search for the class or resource to its parent class loader before attempting to find the class or resource itself.

Class loaders that support concurrent loading of classes are known as parallel capable class loaders and are required to register themselves at their class initialization time by invoking the ClassLoader.registerAsParallelCapable method. Note that the ClassLoader class is registered as parallel capable by default. However, its subclasses still need to register themselves if they are parallel capable. In environments in which the delegation model is not strictly hierarchical, class loaders need to be parallel capable, otherwise class loading can lead to deadlocks because the loader lock is held for the duration of the class loading process (see loadClass methods).

Run-time Built-in Class Loaders

The Java run-time has the following built-in class loaders:

Normally, the Java virtual machine loads classes from the local file system in a platform-dependent manner. However, some classes may not originate from a file; they may originate from other sources, such as the network, or they could be constructed by an application. The method defineClass converts an array of bytes into an instance of class Class. Instances of this newly defined class can be created using Class.newInstance.

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.

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();
       . . .

The network class loader subclass must define the methods findClass and loadClassData to load a class from the network. Once it has downloaded the bytes that make up the class, it should use the method defineClass to create a class instance. 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
             . . .
        }
    }

Binary names

Any class name provided as a String parameter to methods in ClassLoader must be a binary name as defined by The Java Language Specification.

Examples of valid class names include:

  "java.lang.String"
  "javax.swing.JSpinner$DefaultEditor"
  "java.security.KeyStore$Builder$FileBuilder$1"
  "java.net.URLClassLoader$3$1"

Any package name provided as a String parameter to methods in ClassLoader must be either the empty string (denoting an unnamed package) or a fully qualified name as defined by The Java Language Specification.

Since
1.0
Java Language Specification
6.7 Fully Qualified Names, 13.1 The Form of a Binary
See Also
resolveClass(Class)

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class
ClassLoader.ParallelLoaders

Encapsulates the set of parallel capable loader types.

Field Summary

Modifier and TypeField and Description
pack-priv final Object
pack-priv Map<String, Boolean>
private final ArrayList<Class<?>>
private volatile ConcurrentHashMap<?, ?>
private boolean
private final ProtectionDomain
private final NativeLibraries
private final String
private final String
private static final Certificate[]
private final ConcurrentHashMap<String, Certificate[]>
private Map<String, Boolean>
private final ConcurrentHashMap<String, NamedPackage>
private final ConcurrentHashMap<String, Object>
private final ClassLoader
private static volatile ClassLoader
private final Module

Constructor Summary

AccessConstructor and Description
private
ClassLoader(Void unused, String name, ClassLoader parent)

protected
ClassLoader(String
class loader name; or null if not named
name
,
ClassLoader
the parent class loader
parent
)

Creates a new class loader of the specified name and using the specified parent class loader for delegation.

protected
ClassLoader(ClassLoader
The parent class loader
parent
)

Creates a new class loader using the specified parent class loader for delegation.

protected
ClassLoader()

Creates a new class loader using the ClassLoader returned by the method getSystemClassLoader() as the parent class loader.

Method Summary

Modifier and TypeMethod and Description
pack-priv void
private void
pack-priv static void
private static Void
private static Void
private static boolean
private void
public void
clearAssertionStatus()

Sets the default assertion status for this class loader to false and discards any package defaults or class assertion status settings associated with the class loader.

private boolean
compareCerts(Certificate[] pcerts, Certificate[] certs)

check to make sure the certs for the new class (certs) are the same as the certs for the first class inserted in the package (pcerts)

pack-priv ConcurrentHashMap<?, ?>
createOrGetClassLoaderValueMap()

Returns the ConcurrentHashMap used as a storage for ClassLoaderValue(s) associated with this ClassLoader, creating it if it doesn't already exist.

protected final Class<?>

Returns:

The Class object that was created from the specified class data
defineClass
(byte[]
The bytes that make up the class data. The bytes in positions off through off+len-1 should have the format of a valid class file as defined by The Java Virtual Machine Specification.
b
,
int
The start offset in b of the class data
off
,
int
The length of the class data
len
)
Deprecated since 1.1. Replaced by defineClass(String, byte[], int, int)

Converts an array of bytes into an instance of class Class.

protected final Class<?>

Returns:

The Class object that was created from the specified class data.
defineClass
(String
The expected binary name of the class, or null if not known
name
,
byte[]
The bytes that make up the class data. The bytes in positions off through off+len-1 should have the format of a valid class file as defined by The Java Virtual Machine Specification.
b
,
int
The start offset in b of the class data
off
,
int
The length of the class data
len
)

Converts an array of bytes into an instance of class Class.

protected final Class<?>

Returns:

The Class object created from the data, and ProtectionDomain.
defineClass
(String
The expected binary name of the class, or null if not known
name
,
byte[]
The bytes that make up the class data. The bytes in positions off through off+len-1 should have the format of a valid class file as defined by The Java Virtual Machine Specification.
b
,
int
The start offset in b of the class data
off
,
int
The length of the class data
len
,
ProtectionDomain
The ProtectionDomain of the class
protectionDomain
)

Converts an array of bytes into an instance of class Class, with a given ProtectionDomain.

protected final Class<?>

Returns:

The Class object created from the data, and ProtectionDomain.
defineClass
(String
The expected binary name. of the class, or null if not known
name
,
ByteBuffer
The bytes that make up the class data. The bytes from positions b.position() through b.position() + b.limit() -1 should have the format of a valid class file as defined by The Java Virtual Machine Specification.
b
,
ProtectionDomain
The ProtectionDomain of the class, or null.
protectionDomain
)

Converts a ByteBuffer into an instance of class Class, with the given ProtectionDomain.

pack-priv static native Class<?>
defineClass0(ClassLoader
the defining loader
loader
,
Class<?>
nest host of the Class to be defined
lookup
,
String
the binary name or null if not findable
name
,
byte[]
class bytes
b
,
int
the start offset in b of the class bytes
off
,
int
the length of the class bytes
len
,
ProtectionDomain
protection domain
pd
,
boolean
initialize the class
initialize
,
int
flags
flags
,
Object
class data
classData
)

Defines a class of the given flags via Lookup.defineClass.

pack-priv static native Class<?>
defineClass1(ClassLoader loader, String name, byte[] b, int off, int len, ProtectionDomain pd, String source)

pack-priv static native Class<?>
defineClass2(ClassLoader loader, String name, ByteBuffer b, int off, int len, ProtectionDomain pd, String source)

private String
pack-priv Package
definePackage(Class<?> c)

Define a Package of the given Class object.

pack-priv Package
definePackage(String
package name
name
,
Module
module
m
)

Defines a Package of the given name and module This method does not throw IllegalArgumentException.

protected Package

Returns:

The newly defined Package object
definePackage
(String name, String
The specification title
specTitle
,
String
The specification version
specVersion
,
String
The specification vendor
specVendor
,
String
The implementation title
implTitle
,
String
The implementation version
implVersion
,
String
The implementation vendor
implVendor
,
URL
If not null, then this package is sealed with respect to the given code source URL object. Otherwise, the package is not sealed.
sealBase
)

Defines a package by name in this ClassLoader.

pack-priv boolean

Returns:

The desired assertion status of the specified class.
desiredAssertionStatus
(String
The fully qualified class name of the class whose desired assertion status is being queried.
className
)

Returns the assertion status that would be assigned to the specified class if it were to be initialized at the time this method is invoked.

private static native Class<?>
pack-priv static Class<?>
findBootstrapClassOrNull(String name)

Returns a class loaded by the bootstrap class loader; or return null if not found.

protected Class<?>

Returns:

The resulting Class object
findClass
(String
The binary name of the class
name
)

Finds the class with the specified binary name.

protected Class<?>

Returns:

The resulting Class object, or null if the class could not be found.
findClass
(String
The module name; or null to find the class in the unnamed module for this class loader
moduleName
,
String
The binary name of the class
name
)

Finds the class with the given binary name in a module defined to this class loader.

protected String

Returns:

The absolute path of the native library
findLibrary
(String
The library name
libname
)

Returns the absolute path name of a native library.

protected final Class<?>

Returns:

The Class object, or null if the class has not been loaded
findLoadedClass
(String
The binary name of the class
name
)

Returns the class with the given binary name if this loader has been recorded by the Java virtual machine as an initiating loader of a class with that binary name.

private final native Class<?>
pack-priv static long
findNative(ClassLoader loader, String entryName)

protected URL

Returns:

A URL to the resource; null if the resource could not be found, a URL could not be constructed to locate the resource, access to the resource is denied by the security manager, or there isn't a module of the given name defined to the class loader.
findResource
(String
The module name; or null to find a resource in the unnamed module for this class loader
moduleName
,
String
The resource name
name
)

Returns a URL to a resource in a module defined to this class loader.

protected URL

Returns:

URL object for reading the resource; null if the resource could not be found, a URL could not be constructed to locate the resource, the resource is in a package that is not opened unconditionally, or access to the resource is denied by the security manager.
findResource
(String
The resource name
name
)

Finds the resource with the given name.

protected Enumeration<URL>

Returns:

An enumeration of URL objects for the resource. If no resources could be found, the enumeration will be empty. Resources for which a URL cannot be constructed, are in a package that is not opened unconditionally, or access to the resource is denied by the security manager, are not returned in the enumeration.
findResources
(String
The resource name
name
)

Returns an enumeration of URL objects representing all the resources with the given name.

protected final Class<?>

Returns:

The Class object for the specified name
findSystemClass
(String
The binary name of the class
name
)

Finds a class with the specified binary name, loading it if necessary.

pack-priv static ClassLoader
pack-priv static ClassLoader
pack-priv static ClassLoader
getClassLoader(Class<?> caller)

protected Object

Returns:

the lock for class loading operations
getClassLoadingLock
(String
The name of the to-be-loaded class
className
)

Returns the lock object for class loading operations.

public final Package

Returns:

The Package of the given name that has been defined by this class loader, or null if not found
getDefinedPackage
(String name)

Returns a Package of the given name that has been defined by this class loader.

public final Package[]

Returns:

The array of Package objects that have been defined by this class loader; or a zero length array if no package has been defined by this class loader.
getDefinedPackages
()

Returns all of the Packages that have been defined by this class loader.

public String

Returns:

name of this class loader; or null if this class loader is not named.
getName
()

Returns the name of this class loader or null if this class loader is not named.

private NamedPackage
protected Package

Returns:

The Package of the given name that has been defined by this class loader or its ancestors, or null if not found.
getPackage
(String name)
Deprecated since 9. If multiple class loaders delegate to each other and define classes with the same package name, and one such loader relies on the lookup behavior of getPackage to return a Package from a parent loader, then the properties exposed by the Package may not be as expected in the rest of the program.

Finds a package by name in this class loader and its ancestors.

protected Package[]

Returns:

The array of Package objects that have been defined by this class loader and its ancestors
getPackages
()

Returns all of the Packages that have been defined by this class loader and its ancestors.

public final ClassLoader

Returns:

The parent ClassLoader
getParent
()

Returns the parent class loader for delegation.

public static ClassLoader

Returns:

The platform ClassLoader.
getPlatformClassLoader
()

Returns the platform class loader.

public URL

Returns:

URL object for reading the resource; null if the resource could not be found, a URL could not be constructed to locate the resource, the resource is in a package that is not opened unconditionally, or access to the resource is denied by the security manager.
getResource
(String
The resource name
name
)

Finds the resource with the given name.

public InputStream

Returns:

An input stream for reading the resource; null if the resource could not be found, the resource is in a package that is not opened unconditionally, or access to the resource is denied by the security manager.
getResourceAsStream
(String
The resource name
name
)

Returns an input stream for reading the specified resource.

public Enumeration<URL>

Returns:

An enumeration of URL objects for the resource. If no resources could be found, the enumeration will be empty. Resources for which a URL cannot be constructed, are in a package that is not opened unconditionally, or access to the resource is denied by the security manager, are not returned in the enumeration.
getResources
(String
The resource name
name
)

Finds all the resources with the given name.

public static ClassLoader

Returns:

The system ClassLoader
getSystemClassLoader
()

Returns the system class loader.

public static URL

Returns:

A URL to the resource; null if the resource could not be found, a URL could not be constructed to locate the resource, the resource is in a package that is not opened unconditionally or access to the resource is denied by the security manager.
getSystemResource
(String
The resource name
name
)

Find a resource of the specified name from the search path used to load classes.

public static InputStream

Returns:

An input stream for reading the resource; null if the resource could not be found, the resource is in a package that is not opened unconditionally, or access to the resource is denied by the security manager.
getSystemResourceAsStream
(String
The resource name
name
)

Open for reading, a resource of the specified name from the search path used to load classes.

public static Enumeration<URL>

Returns:

An enumeration of URL objects for the resource. If no resources could be found, the enumeration will be empty. Resources for which a URL cannot be constructed, are in a package that is not opened unconditionally, or access to the resource is denied by the security manager, are not returned in the enumeration.
getSystemResources
(String
The resource name
name
)

Finds all resources of the specified name from the search path used to load classes.

public final Module

Returns:

The unnamed Module for this class loader
getUnnamedModule
()

Returns the unnamed Module for this class loader.

private void
pack-priv static synchronized ClassLoader
pack-priv boolean
public final boolean

Returns:

true if this class loader is parallel capable, otherwise false.
isRegisteredAsParallelCapable
()

Returns true if this class loader is registered as parallel capable, otherwise false.

public Class<?>

Returns:

The resulting Class object
loadClass
(String
The binary name of the class
name
)

Loads the class with the specified binary name.

protected Class<?>

Returns:

The resulting Class object
loadClass
(String
The binary name of the class
name
,
boolean
If true then resolve the class
resolve
)

Loads the class with the specified binary name.

pack-priv final Class<?>

Returns:

The resulting Class object in a module defined by this class loader, or null if the class could not be found.
loadClass
(Module
The module
module
,
String
The binary name of the class
name
)

Loads the class with the specified binary name in a module defined to this class loader.

pack-priv static NativeLibrary
loadLibrary(Class<?> fromClass, File file)

pack-priv static NativeLibrary
loadLibrary(Class<?> fromClass, String name)

pack-priv final String
name()

private static String
nameAndId(ClassLoader ld)

If the defining loader has a name explicitly set then '<loader-name>' @<id> If the defining loader has no name then <qualified-class-name> @<id> If it's built-in loader then omit `@<id>` as there is only one instance.

pack-priv String
private static boolean
pack-priv Stream<Package>
packages()

Returns a stream of Packages defined in this class loader

private void
private ProtectionDomain
protected static boolean

Returns:

true if the caller is successfully registered as parallel capable and false if otherwise.
registerAsParallelCapable
()

Registers the caller as parallel capable.

private static boolean
private static native void
private void
resetArchivedStates()

Called by the VM, during -Xshare:dump

protected final void
resolveClass(Class<?>
The class to link
c
)

Links the specified class.

public Stream<URL>

Returns:

A stream of resource URL objects. If no resources could be found, the stream will be empty. Resources for which a URL cannot be constructed, are in a package that is not opened unconditionally, or access to the resource is denied by the security manager, will not be in the stream.
resources
(String
The resource name
name
)

Returns a stream whose elements are the URLs of all the resources with the given name.

private static native AssertionStatusDirectives
public void
setClassAssertionStatus(String
The fully qualified class name of the top-level class whose assertion status is to be set.
className
,
boolean
true if the named class is to have assertions enabled when (and if) it is initialized, false if the class is to have assertions disabled.
enabled
)

Sets the desired assertion status for the named top-level class in this class loader and any nested classes contained therein.

public void
setDefaultAssertionStatus(boolean
true if classes loaded by this class loader will henceforth have assertions enabled by default, false if they will have assertions disabled by default.
enabled
)

Sets the default assertion status for this class loader.

public void
setPackageAssertionStatus(String
The name of the package whose package default assertion status is to be set. A null value indicates the unnamed package that is "current" (see section 7.4.2 of The Java Language Specification.)
packageName
,
boolean
true if classes loaded by this classloader and belonging to the named package or any of its subpackages will have assertions enabled by default, false if they will have assertions disabled by default.
enabled
)

Sets the package default assertion status for the named package.

protected final void
setSigners(Class<?>
The Class object
c
,
Object[]
The signers for the class
signers
)

Sets the signers of a class.

private Package
private boolean
trySetObjectField(String name, Object obj)

Attempts to atomically set a volatile field in this object.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait