|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Object![]()
![]()
![]()
java.lang.ClassLoader

public abstract class ClassLoader

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.
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 delegate the search for the class or resource to its parent class loader before attempting to find the class or resource itself. The virtual machine's built-in class loader, called the "bootstrap class loader", does not itself have a parent but may serve as the parent of a ClassLoader instance.
Normally, the Java virtual machine loads classes from the local file system in a platform-dependent manner. For example, on UNIX systems, the virtual machine loads classes from the directory defined by the CLASSPATH environment variable.
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
. . .
}
}
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"
resolveClass(Class)

| Constructor Summary | |
|---|---|
protected |
ClassLoader
Creates a new class loader using the ClassLoader returned by the method getSystemClassLoader() as the parent class loader. |
protected |
ClassLoader
Creates a new class loader using the specified parent class loader for delegation. |
| Method Summary | |
|---|---|
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. |
protected Class |
defineClass
Deprecated. Replaced by defineClass(String, byte[], int, int) ![]() |
protected Class |
defineClass
Converts an array of bytes into an instance of class Class. |
protected Class |
defineClass
Converts an array of bytes into an instance of class Class, with an optional ProtectionDomain. |
protected Class |
defineClass
Converts a ByteBuffer
into an instance of class Class,
with an optional ProtectionDomain. |
protected Package |
definePackage
Defines a package by name in this ClassLoader. |
protected Class |
findClass
Finds the class with the specified binary name. |
protected String |
findLibrary
Returns the absolute path name of a native library. |
protected Class |
findLoadedClass
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. |
protected URL |
findResource
Finds the resource with the given name. |
protected Enumeration |
findResources
Returns an enumeration of URL objects
representing all the resources with the given name. |
protected Class |
findSystemClass
Finds a class with the specified binary name, loading it if necessary. |
protected Package |
getPackage
Returns a Package that has been defined by this class loader or any of its ancestors. |
protected Package |
getPackages
Returns all of the Packages defined by this class loader and its ancestors. |
ClassLoader |
getParent
Returns the parent class loader for delegation. |
URL |
getResource
Finds the resource with the given name. |
InputStream |
getResourceAsStream
Returns an input stream for reading the specified resource. |
Enumeration |
getResources
Finds all the resources with the given name. |
static ClassLoader |
getSystemClassLoader
Returns the system class loader for delegation. |
static URL |
getSystemResource
Find a resource of the specified name from the search path used to load classes. |
static InputStream |
getSystemResourceAsStream
Open for reading, a resource of the specified name from the search path used to load classes. |
static Enumeration |
getSystemResources
Finds all resources of the specified name from the search path used to load classes. |
Class |
loadClass
Loads the class with the specified binary name. |
protected Class |
loadClass
Loads the class with the specified binary name. |
protected void |
resolveClass
Links the specified class. |
void |
setClassAssertionStatus
Sets the desired assertion status for the named top-level class in this class loader and any nested classes contained therein. |
void |
setDefaultAssertionStatus
Sets the default assertion status for this class loader. |
void |
setPackageAssertionStatus
Sets the package default assertion status for the named package. |
protected void |
setSigners
Sets the signers of a class. |
Methods inherited from class java.lang.Object ![]() |
|---|
clone |
| Constructor Detail |
|---|

protected ClassLoader(ClassLoader![]()
![]()
parent)
If there is a security manager, its checkCreateClassLoader
method is invoked. This may result in
a security exception.
parent - The parent class loader
SecurityException

- If a security manager exists and its
checkCreateClassLoader method doesn't allow creation
of a new class loader.

protected ClassLoader()
getSystemClassLoader()
as the parent class loader.
If there is a security manager, its checkCreateClassLoader
method is invoked. This may result in
a security exception.
SecurityException

- If a security manager exists and its
checkCreateClassLoader method doesn't allow creation
of a new class loader.| Method Detail |
|---|

public Class![]()
![]()
<?> loadClass(String
![]()
![]()
name) throws ClassNotFoundException
![]()
![]()
loadClass(String, boolean)
method. It is invoked by the Java virtual
machine to resolve class references. Invoking this method is equivalent
to invoking loadClass(name,
false)
.
name - The binary name of the class
ClassNotFoundException

- If the class was not found

protected Class![]()
![]()
<?> loadClass(String
![]()
![]()
name, boolean resolve) throws ClassNotFoundException
![]()
![]()
Invoke findLoadedClass(String)
to check if the class
has already been loaded.
Invoke the loadClass
method
on the parent class loader. If the parent is null the class
loader built-in to the virtual machine is used, instead.
Invoke the findClass(String)
method to find the
class.
If the class was found using the above steps, and the
resolve flag is true, this method will then invoke the resolveClass(Class)
method on the resulting Class object.
Subclasses of ClassLoader are encouraged to override findClass(String)
, rather than this method.
name - The binary name of the classresolve - If true then resolve the class
ClassNotFoundException

- If the class could not be found

protected Class![]()
![]()
<?> findClass(String
![]()
![]()
name) throws ClassNotFoundException
![]()
![]()
loadClass
method after checking the
parent class loader for the requested class. The default implementation
throws a ClassNotFoundException.
name - The binary name of the class
ClassNotFoundException

- If the class could not be found

@Deprecated protected final Class![]()
![]()
<?> defineClass(byte[] b, int off, int len) throws ClassFormatError
![]()
![]()
defineClass(String, byte[], int, int)
b - 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.off - The start offset in b of the class datalen - The length of the class data
ClassFormatError

- If the data did not contain a valid class
IndexOutOfBoundsException

- If either off or len is negative, or if
off+len is greater than b.length.loadClass(String, boolean)
,
resolveClass(Class)


protected final Class![]()
![]()
<?> defineClass(String
![]()
![]()
name, byte[] b, int off, int len) throws ClassFormatError
![]()
![]()
This method assigns a default Protecti