All Packages  Class Hierarchy  This Package  Previous  Next  Index

Class java.lang.ClassLoader

java.lang.Object
   |
   +----java.lang.ClassLoader

public class ClassLoader
extends Object
ClassLoader is an abstract Class that can be used to define a policy for loading Java classes into the runtime environment. By default, the runtime system loads classes that originate as files by reading them from the directory defined by the CLASSPATH environment variable (this is platform dependent). The default mechanism does not involve a Class loader.

However, some classes may not originate from a file; they could be loaded from some other source, e.g., the network. Classes loaded from the network are an array of bytes. A ClassLoader can be used to tell the runtime system to convert an array of bytes into an instance of class Class. This conversion information is passed to the runtime using the defineClass() method.

Classes that are created through the defineClass() mechanism can reference other classes by name. To resolve those names, the runtime system calls the ClassLoader that originally created the Class. The runtime system calls the abstract method loadClass() to load the referenced classes.

 	ClassLoader loader = new NetworkClassLoader(host, port);
  	Object main = loader.loadClass("Main").newInstance();
	....
 
The NetworkClassLoader subclass must define the method loadClass() 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 could be:
	class NetworkClassLoader {
	    String host;
	    int port;
	    Hashtable cache = new Hashtable();
	    private byte loadClassData(String name)[] {
		// load the class data from the connection
		...
	    }
	    public synchronized Class loadClass(String name) {
	        Class c = cache.get(name);
		if (c == null) {
		    byte data[] = loadClassData(name);
		    cache.put(name, defineClass(name, data, 0, data.length));
		}
		return c;
	    }
	}
 

See Also:
Class

Constructor Index

 o ClassLoader()
Constructs a new Class loader and initializes it.

Method Index

 o defineClass(byte[], int, int)
Converts an array of bytes to an instance of class Class.
 o defineClass(String, byte[], int, int)
Converts an array of bytes to an instance of class Class.
 o findLoadedClass(String)
 o findSystemClass(String)
Loads a system Class.
 o getResourceAsName(String)
Find a resource with a given name.
 o getResourceAsStream(String)
Get an InputStream on a given resource.
 o getSystemResourceAsName(String)
Find a resource with a given name.
 o getSystemResourceAsStream(String)
Get an InputStream on a given resource..
 o loadClass(String)
Resolves the specified name to a Class.
 o loadClass(String, boolean)
Resolves the specified name to a Class.
 o resolveClass(Class)
Resolves classes referenced by this Class.
 o setSigners(Class, Object[])
Sets the signers of a class.

Constructors

 o ClassLoader
  protected ClassLoader()
Constructs a new Class loader and initializes it.

Methods

 o loadClass
  public Class loadClass(String name) throws ClassNotFoundException
Resolves the specified name to a Class.

Parameters:
name - the name of the desired Class
Returns:
the resulting Class, or null if it was not found.
Throws: ClassNotFoundException
Cannot find a definition for the class
 o loadClass
  protected abstract Class loadClass(String name,
                                     boolean resolve) throws ClassNotFoundException
Resolves the specified name to a Class. The method loadClass() is called by the virtual machine. As an abstract method, loadClass() must be defined in a subclass of ClassLoader. By using a Hashtable, you can avoid loading the same Class more than once.

Parameters:
name - the name of the desired Class
resolve - true if the Class needs to be resolved
Returns:
the resulting Class, or null if it was not found.
Throws: ClassNotFoundException
Cannot find a definition for the class
See Also:
Hashtable
 o defineClass
  protected final Class defineClass(byte data[],
                                    int offset,
                                    int length)
Converts an array of bytes to an instance of class Class. Before the Class can be used it must be resolved. This method is deprecated in favor of the version that takes a "name" as a first argument, and is more secure.

Parameters:
data - the bytes that make up the Class
offset - the start offset of the Class data
length - the length of the Class data
Returns:
the Class object which was created from the data.
Throws: ClassFormatError
If the data does not contain a valid Class.
See Also:
loadClass, resolveClass
 o defineClass
  protected final Class defineClass(String name,
                                    byte data[],
                                    int offset,
                                    int length)
Converts an array of bytes to an instance of class Class. Before the Class can be used it must be resolved.

Parameters:
name - the expected name of the class; null if unknown; using '.' and not '/' as separator, and without a trailing ".class" suffix.
data - the bytes that make up the Class
offset - the start offset of the Class data
length - the length of the Class data
Returns:
the Class object which was created from the data.
Throws: ClassFormatError
If the data does not contain a valid Class.
See Also:
loadClass, resolveClass
 o resolveClass
  protected final void resolveClass(Class c)
Resolves classes referenced by this Class. This must be done before the Class can be used. Class names referenced by the resulting Class are resolved by calling loadClass().

Parameters:
c - the Class to be resolved
See Also:
defineClass
 o findSystemClass
  protected final Class findSystemClass(String name) throws ClassNotFoundException
Loads a system Class. A system Class is a class with the primordial Class loader (which is null).

Parameters:
name - the name of the system Class
Throws: NoClassDefFoundError
If the Class is not found.
Throws: ClassNotFoundException
Cannot find a definition for the class
 o setSigners
  protected final void setSigners(Class cl,
                                  Object signers[])
Sets the signers of a class. This is called after defining a class, by signature-aware class loading code.

 o findLoadedClass
  protected final Class findLoadedClass(String name)
 o getSystemResourceAsStream
  public final static InputStream getSystemResourceAsStream(String name)
Get an InputStream on a given resource.. Will return null if no resource with this name is found.

The resource name may be any system resource (e.g. follows CLASSPATH order)

Parameters:
name - the name of the resource, to be used as is.
Returns:
an InputStream on the resource, or null if not found.
 o getSystemResourceAsName
  public final static String getSystemResourceAsName(String name)
Find a resource with a given name. The return is the external representation of an URL the resource. Doing a getContent() on the URL may return an Image, an AudioClip, or an InputStream.

The resource name may be any system resource (e.g. follows CLASSPATH order)

Parameters:
name - the name of the resource, to be used as is.
Returns:
the external representation of a URL on the resource, or null if not found.
 o getResourceAsStream
  public InputStream getResourceAsStream(String name)
Get an InputStream on a given resource. Will return null if no resource with this name is found.

The class loader can choose what to do to locate the resource.

Parameters:
name - the name of the resource, to be used as is.
Returns:
an InputStream on the resource, or null if not found.
 o getResourceAsName
  public String getResourceAsName(String name)
Find a resource with a given name. The return is the external representation of an URL the resource. Doing a getContent() on the URL may return an Image, an AudioClip, or an InputStream.

The class loader can choose what to do to locate the resource.

Parameters:
name - the name of the resource, to be used as is.
Returns:
an InputStream on the resource, or null if not found.

All Packages  Class Hierarchy  This Package  Previous  Next  Index