Package java.lang Previous
Previous
Java API
Java API
Index
Index
Next
Next

Class ClassLoader

Constructors , Methods

public  abstract class  java.lang.ClassLoader
    extends  java.lang.Object  
{
        // Constructors
    protected ClassLoader();	

        // Methods
    protected final Class 	
        defineClass(byte  data[], int  offset, int  length);
    protected final Class findSystemClass(String  name);	
    protected abstract Class 	
        loadClass(String  name, boolean  resolve);
    protected final void resolveClass(Class  c);	
}

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

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 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 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:

ClassLoader loader = new NetworkClassLoader(host, port); Object main = loader.loadClass("Main", true).newInstance(); .... 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 is called. This may result in a security exception .

Throw:

SecurityException

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.

Return Value:

Returns the class object which was created from the data.

ParameterDescription
data the bytes that make up the Class
offset the start offset of the Class data
length the length of the Class data

Throw:

ClassFormatError

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.

Return Value:

Returns a system class with the given name.

ParameterDescription
name the name of the system Class

Throw:

NoClassDefFoundError

If the Class is not found.

Throw:

ClassNotFoundException

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.

Return Value:

Returns the resulting Class, or null if it was not found.

ParameterDescription
name the name of the desired Class
resolve true if the Class must be resolved

Throw:

ClassNotFoundException

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.

ParameterDescription
c the Class instance to be resolved



Top© 1996 Sun Microsystems, Inc. All rights reserved.