Packages  This Package  Prev  Next  Index  

§1.4 Class ClassLoader

public  abstract class  java.lang.ClassLoader
    extends  java.lang.Object  (I-§1.12)
{
        // Constructors
    protected ClassLoader();	§1.4.1

        // Methods
    protected final Class 	§1.4.2
        defineClass(byte  data[], int  offset, int  length);
    protected final Class findSystemClass(String  name);	§1.4.3
    protected abstract Class 	§1.4.4
        loadClass(String  name, boolean  resolve);
    protected final void resolveClass(Class  c);	§1.4.5
}
The class ClassLoader is an abstract class. Applications implement subclasses of ClassLoader in order to extend the manner in which the Java Virtual Machine dynamically loads classes.

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 (I-§1.4.2) converts an array of bytes into an instance of class Class (I-§1.3). Instances of this newly defined class can be created using the newInstance method in class Class (I-§1.3.7).

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 calls the loadClass method (I-§1.4.4) of the class loader that originally created the class. If the Java Virtual Machine only needs to determine if the class exists and if it does exist to know its superclass, the resolve flag is set to false. However if an instance of the class is being created or any of its methods are being called, the class must also be resolved. In this case the resolve flag is set to true, and the resolveClass method (I-§1.4.5) should be called.

For example, an application could create a network class loader to download class files from a server. Sample code might look like:

The network class loader 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 might be the following:


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,
boolean resolve) { Class c = cache.get(name); if (c == null) { byte data[] = loadClassData(name); c = defineClass(data, 0, data.length); cache.put(name, c); } if (resolve) resolveClass(c); return c;
}
}

Constructors

ClassLoader

protected ClassLoader()
Constructs a new class loader and initializes it.
If there is a security manager, its checkCreateClassLoader method (I-§1.15.8) is called. This may result in a security exception (I-§1.43).

Throws
SecurityException (I-§1.43)
If the current thread does not have permission to create a new class loader.

Methods

defineClass

protected final Class
defineClass(byte data[], int offset, int length)
Converts an array of bytes into an instance of class Class.
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 (I-§1.47)
If the data does not contain a valid Class.

findSystemClass

protected final Class findSystemClass(String name) throws ClassNotFoundException
Finds the system class with the specified name, loading it in if necessary.
A system class is a class loaded from the local file system in a platform- dependent way. It has no class loader.
Parameters:
name - the name of the system Class
Returns:
a system class with the given name.
Throws
NoClassDefFoundError (I-§1.54)
If the Class is not found.
Throws
ClassNotFoundException (I-§1.28)
If it could not find a definition for the class

loadClass

protected abstract Class
loadClass(String name, boolean resolve)
throws ClassNotFoundException
Requests the class loader to load a class with the specified name. The loadClass method is called by the Java Virtual Machine when a class loaded by a class loader first references another class. Every subclass of class ClassLoader must define this method.
If the resolve flag is true, the method should call the resolveClass method on the resulting class object.
Class loaders should use a hash table or other cache to avoid defining classes with the same name multiple times.
Parameters:
name - the name of the desired Class
resolve - true if the Class must be resolved
Returns:
the resulting Class, or null if it was not found.
Throws
ClassNotFoundException (I-§1.28)
If the classloader cannot find a definition for the class

resolveClass

protected final void resolveClass(Class c)
Resolves the class so that an instance of the class can be created, or so that one of its methods can be called. This method should be called by loadClass if the resolve flag is true.
Parameters:
c - the Class instance to be resolved

Packages  This Package  Prev  Next  Index
Java API Document (HTML generated by dkramer on April 22, 1996)
Copyright © 1996 Sun Microsystems, Inc. All rights reserved
Please send any comments or corrections to doug.kramer@sun.com