ClassLoader

Name

ClassLoader

Synopsis

Description

The ClassLoader class provides a mechanism for Java to load classes over a network or from any source other than the local filesystem. The default class-loading mechanism loads classes from files found relative to directories specified by the CLASSPATH environment variable. This default mechanism does not use an instance of the ClassLoader class.

An application can implement another mechanism for loading classes by declaring a subclass of the abstract ClassLoader class. A subclass of ClassLoader must override the loadClass() to define a class-loading policy. This method implements any sort of security that is necessary for the class-loading mechanism. The other methods of ClassLoader are final, so they cannot be overridden.

A ClassLoader object is typically used by calling its loadClass() method to explicitly load a top-level class, such as a subclass of Applet. The ClassLoader that loads the class becomes associated with the class; it can be obtained by calling the getClassLoader() method of the Class object that represents the class.

Once a class is loaded, it must be resolved before it can be used. Resolving a class means ensuring that all of the other classes it references are loaded. In addition, all of the classes that they reference must be loaded, and so on, until all of the needed classes have been loaded. Classes are resolved using the resolveClass() method of the ClassLoader object that loaded the initial class. This means that when a ClassLoader object is explicitly used to load a class, the same ClassLoader is used to load all of the classes that it references, directly or indirectly.

Classes loaded using a ClassLoader object may attempt to load additional classes without explicitly using a ClassLoader object. They can do this by calling the Class class' forName() method. However, in such a situation, a ClassLoader object is implicitly used. See the description of Class.forName() for more information.

Java identifies a class by a combination of its fully qualified name and the class loader that was used to load the class. If you write a subclass of ClassLoader, it should not attempt to directly load local classes. Instead, it should call findSystemClass(). A local class that is loaded directly by a ClassLoader is considered to be a different class than the same class loaded by findSystemClass(). This can lead to having two copies of the same class loaded, which can cause a number of inconsistencies. For example, the class' equals() method may decide that the same object is not equal to itself.

Class Summary

public abstract class java.lang.ClassLoader extends java.lang.Object {
 // Constructors protected ClassLoader(); // Class Methods public static final URL getSystemResource(String name); // New in 1.1 public static final InputStream getSystemResourceAsStream(String name); // New in 1.1 // Public Instance Methods public URL getResource(String name); // New in 1.1 public InputStream getResourceAsStream(String name); // New in 1.1 public Class loadClass(String name); // New in 1.1 // Protected Instance Methods protected final Class defineClass(byte data[], int offset, int length); // Deprecated in 1.1 protected final Class defineClass(String name, byte[] data, int offset, int length); // New in 1.1 protected final Class findLoadedClass(String name); // New in 1.1 protected final Class findSystemClass(String name); protected abstract Class loadClass(String name, boolean resolve); protected final void resolveClass(Class c); protected final void setSigners(Class cl, Object[] signers); // New in 1.1
}

Constructors

ClassLoader

protected ClassLoader()

Class Methods

getSystemResource

public static final URL getSystemResource(String name)

getSystemResourceAsStream

public static final InputStream getSystemResourceAsStream(String name)

Public Instance Methods

getResource

public URL getResource(String name)

getResourceAsStream

public InputStream getResourceAsStream(String name)

loadClass

public Class loadClass(String name) throws ClassNotFoundException

Protected Instance Methods

defineClass

protected final Class defineClass(byte data[], int offset, int length) 

protected final Class defineClass(String name, byte data[], int offset, int length) 

findLoadedClass

protected final Class findLoadedClass(String name) 

findSystemClass

protected final Class findSystemClass(String name) throws ClassNotFoundException 

loadClass

protected abstract Class loadClass(String name, boolean resolve) throws ClassNotFoundException 

resolveClass

protected final void resolveClass(Class c)

setSigners

protected final void setSigners(Class cl, Object[] signers) 

Inherited Methods

Method Inherited From Method Inherited From
clone() Object equals(Object) Object
finalize() Object getClass() Object
hashCode() Object notify() Object
notifyAll() Object toString() Object
wait() Object wait(long) Object
wait(long, int) Object

See Also

Class; Errors; Exceptions; Object; SecurityManager