Class java.lang.ClassLoader
All Packages    This Package    Previous    Next

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 to 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 definedClass 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(data, 0, data.length));
		}
		return c;
	    }
	}
See Also:
Class
Version:
1.14, 21 Feb 1995
Author:
Arthur van Hoff

ClassLoader()
Constructs a new class loader and initializes it.

defineClass(byte[], int, int)
Converts an array of bytes to an instance of class Class.
getClassContext()
Returns the classes that are currently on the execution stack.
loadClass(String, boolean)
Resolves the specified name to a class.
resolveClass(Class)
Resolves classes referenced by this class.

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

loadClass
  protected abstract Class loadClass(String name,
                                     boolean resolve)
Resolves the specified name to a class. loadClass() is called by the interpreter. As an abstract method, loadClass() must be defined in a subclass of ClassLoader. Using a Hashtable avoids loading the same class more than once. It is an abstract method, it must therefore be defined in a subclass.
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
See Also:
Hashtable

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.
Parameters:
data - the bytes that make up the class
offset - the start ofset of the class data
length - the length of the class data
Returns:
the class object which was created from the data
Throws: FileFormatException
the data does not contain a valid class
See Also:
loadClass, resolveClass

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

getClassContext

  public static Class getClassContext()
Returns the classes that are currently on the execution stack. This is a hack and will probably not be supported in the future.


All Packages    This Package    Previous    Next