All Packages This Package Previous Next
java.lang.Object | +----java.lang.ClassLoader
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(data, 0, data.length));
}
return c;
}
}
protected ClassLoader()
protected abstract Class loadClass(String name,
boolean resolve) throws ClassNotFoundException
protected final Class defineClass(byte data[],
int offset,
int length)
protected final void resolveClass(Class c)
protected final Class findSystemClass(String name) throws ClassNotFoundException
All Packages This Package Previous Next