Contents | Package | Class | Tree | Deprecated | Index | Help Java 1.2 Beta 3
PREV | NEXT SHOW LISTS | HIDE LISTS

Class java.lang.ClassLoader

java.lang.Object
    |
    +----java.lang.ClassLoader
Subclasses:
SecureClassLoader

public abstract class ClassLoader
extends Object
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 is:


     class NetworkClassLoader extends ClassLoader {
         String host;
         int port;

         public Class findLocalClass(String name) {
             byte[] b = loadClassData(name);
             return defineClass(name, b, 0, b.length);
         }

         private byte[] loadClassData(String name) {
             // load the class data from the connection
              . . .
         }
     }
 

Since:
JDK1.0
See Also:
Class, newInstance(), defineClass(byte[], int, int), loadClass(java.lang.String, boolean), resolveClass(java.lang.Class)

Constructor Summary
 ClassLoader(ClassLoader parent)
Creates a new class loader using the specified parent class loader for delegation.
 ClassLoader()
Creates a new class loader using the ClassLoader returned by the method getBaseClassLoader() as the parent class loader.
 

Method Summary
void  checkPackageAccess(String name)
This method can be overridden to throw a SecurityException if the calling thread is not allowed to access the package specified by the argument.
Class  defineClass(byte[] b, int off, int len)
Converts an array of bytes into an instance of class Class. Deprecated
Class  defineClass(String name, byte[] b, int off, int len)
Converts an array of bytes into an instance of class Class.
Package  definePackage(String name, String specTitle, String specVersion, String specVendor, String implTitle, String implVersion, String implVendor, URL sealBase)
Defines a package by name in this ClassLoader.
Class  findLoadedClass(String name)
Finds the class with the given name if it had been previously loaded through this class loader.
Class  findLocalClass(String name)
Finds the specified class locally.
Class  findSystemClass(String name)
Finds the system class with the specified name, loading it in if necessary.
static ClassLoader  getBaseClassLoader()
Returns the base class loader for delegation.
URL  getLocalResource(String name)
Finds the local resource with the given name.
Enumeration  getLocalResources(String name)
Returns an Enumeration of URLs representing all the local resources with the given name.
Package  getPackage(String name)
Returns the Package defined in this ClassLoader for the given name.
Package[]  getPackages()
Returns all of the Packages defined in this ClassLoader.
ClassLoader  getParent()
Returns the parent class loader for delegation, or null if none, in which case ClassLoader will delegate to the system class loader.
URL  getResource(String name)
Finds the resource with the given name.
InputStream  getResourceAsStream(String name)
Returns an input stream for reading the specified resource.
Enumeration  getResources(String name)
Finds all the resources with the given name.
static URL  getSystemResource(String name)
Finds the system resource with the given name.
static InputStream  getSystemResourceAsStream(String name)
Returns an input stream for reading the specified system resource.
static Enumeration  getSystemResources(String name)
Returns an enumeration of URLs specifying all of the system resources with the specified name.
Class  loadClass(String name)
Loads the class with the specified name.
Class  loadClass(String name, boolean resolve)
Loads the class with the specified name.
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.
void  setSigners(Class c, Object[] signers)
Sets the signers of a class.
 
Methods inherited from class java.lang.Object
 clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ClassLoader

protected ClassLoader(ClassLoader parent)
Creates a new class loader using the specified parent class loader for delegation.

If there is a security manager, its checkCreateClassLoader method is called. This may result in a security exception.

Throws:
SecurityException - if the current thread does not have permission to create a new class loader
See Also:
SecurityException, checkCreateClassLoader()

ClassLoader

protected ClassLoader()
Creates a new class loader using the ClassLoader returned by the method getBaseClassLoader() as the parent class loader.

If there is a security manager, its checkCreateClassLoader method is called. This may result in a security exception.

Throws:
SecurityException - if the current thread does not have permission to create a new class loader
See Also:
SecurityException, checkCreateClassLoader()
Method Detail

loadClass

public Class loadClass(String name) throws ClassNotFoundException
Loads the class with the specified name. The resolveClass method will be called on the resulting Class object.
Parameters:
name - the name of the class
Returns:
the resulting Class object
Throws:
ClassNotFoundException - if the class was not found
See Also:
resolveClass

loadClass

protected Class loadClass(String name,
                          boolean resolve) throws ClassNotFoundException
Loads the 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. The default implementation of loadClass will search for classes in the following order:
  1. Call findLoadedClass to check if the class has already been loaded.

  2. Call the loadClass method of the parent class loader to load the class, or findSystemClass if no parent class loader was specified.

  3. Call the findLocalClass method to find the class locally.
If the class was found using the above steps, and the resolve flag is true, this method will then call the resolveClass method on the resulting class object.

Class loader implementations that use the new delegation model introduced in 1.2 should override the findLocalClass method rather than loadClass.

Parameters:
name - the name of the class
resolve - if true then resolve the class
Returns:
the resulting Class object
Throws:
ClassNotFoundException - if the class could not be found

checkPackageAccess

protected void checkPackageAccess(String name) throws SecurityException
This method can be overridden to throw a SecurityException if the calling thread is not allowed to access the package specified by the argument. By default, the calling thread is allowed to access any package.

For example, in order to perform this check using the current SecurityManager:

	    SecurityManager sm = System.getSecurityManager();
	    if (sm != null) {
	        sm.checkPackageAccess(name.substring(0, i));
      }
 
Parameters:
name - the package name
Throws:
SecurityException - if the caller cannot access the specified package

findLocalClass

protected Class findLocalClass(String name) throws ClassNotFoundException
Finds the specified class locally. This method should be overridden by class loader implementations that follow the new delegation model for loading classes, and will be called by the loadClass method after checking the parent class loader for the requested class. The default implementation throws ClassNotFoundException.
Parameters:
name - the name of the class
Returns:
the resulting Class object
Throws:
ClassNotFoundException - if the class could not be found

defineClass

protected final Class defineClass(byte[] b,
                                  int off,
                                  int len) throws ClassFormatError
Note: defineClass() is deprecated.Replaced by defineClass(java.lang.String, byte[], int, int)

Converts an array of bytes into 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 the class name as its first argument, and is more secure.
Parameters:
b - the bytes that make up the class data
off - the start offset of the class data
len - the length of the class data
Returns:
the Class object that was created from the specified class data
Throws:
ClassFormatError - if the data did not contain a valid class
See Also:
loadClass(java.lang.String, boolean), resolveClass(java.lang.Class)

defineClass

protected final Class defineClass(String name,
                                  byte[] b,
                                  int off,
                                  int len) throws ClassFormatError
Converts an array of bytes into an instance of class Class. Before the Class can be used it must be resolved.

This method assigns a default ProtectionDomain to the newly defined class. The ProtectionDomain contains the set of permissions granted when a call to Policy.getPolicy().evaluate() is made with a Codesource of null,null. The default domain is created on the first invocation of defineClass, and re-used on subsequent calls.

Note: There is a similar defineClass method in java.security.SecureClassLoader (a ClassLoader subclass) that assigns the new class to a passed-in ProtectionDomain. Also, a class can be assigned to a new ProtectionDomain (one with different permissions) by a call to the java.lang.Class setProtectionDomain method, if the caller has permission to reset the domain.

Parameters:
name - the expected name of the class, or null if not known, using '.' and not '/' as the separator and without a trailing ".class" suffix.
b - the bytes that make up the class data
off - the start offset of the class data
len - the length of the class data
Returns:
the Class object that was created from the specified class data
Throws:
ClassFormatError - if the data did not contain a valid class
See Also:
loadClass(java.lang.String, boolean), resolveClass(java.lang.Class), ProtectionDomain, Policy, CodeSource, SecureClassLoader

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 will be called by loadClass if the resolve flag is true.
Parameters:
c - the class to be resolved
See Also:
defineClass(java.lang.String,byte[],int,int)

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 manner, and has no associated class loader.

Parameters:
name - the name of the system class
Returns:
a system class with the given name
Throws:
ClassNotFoundException - if the class could not be found

getParent

public ClassLoader getParent()
Returns the parent class loader for delegation, or null if none, in which case ClassLoader will delegate to the system class loader.
Throws:
SecurityException - if the caller does not have RuntimePermission("getClassLoader") permission

setSigners

protected final void setSigners(Class c,
                                Object[] signers)
Sets the signers of a class. This should be called after defining a class.
Parameters:
c - the Class object
signers - the signers for the class

findLoadedClass

protected final Class findLoadedClass(String name)
Finds the class with the given name if it had been previously loaded through this class loader.
Parameters:
name - the class name
Returns:
the Class object, or null if the class has not been loaded

getResource

public URL getResource(String name)
Finds the resource with the given name. A resource is some data (images, audio, text, etc) that can be accessed by class code in a way that is independent of the location of the code.

The name of a resource is a "/"-separated path name that identifies the resource.

This method will first search the parent class loader for the resource, then call getLocalResource to find the resource locally.

Parameters:
name - resource name
Returns:
a URL for reading the resource, or null if the resource could not be found or the caller doesn't have adequate privileges to get the resource.
See Also:
getLocalResource

getResources

public final Enumeration getResources(String name) throws IOException
Finds all the resources with the given name. A resource is some data (images, audio, text, etc) that can be accessed by class code in a way that is independent of the location of the code.

The name of a resource is a "/"-separated path name that identifies the resource.

This method will first search the parent class loader for the resource, then call getLocalResource to find the resource locally.

Parameters:
name - resource name
Returns:
an enumeration of URL to the resource. If no resources could be found, the enumeration will be empty. Resources that the doesn't have access to will not be in the enumeration.
See Also:
getResource, getLocalResources

getLocalResources

public Enumeration getLocalResources(String name) throws IOException
Returns an Enumeration of URLs representing all the local resources with the given name. Class loader implementations should override this method to load resources from a local source.
Parameters:
name - the resource name
Returns:
an Enumeration of URLs for the local resources

getLocalResource

public URL getLocalResource(String name)
Finds the local resource with the given name. Class loader implementations should override this method to load a resource from a local source.
Parameters:
name - the resource name
Returns:
a URL for reading the resource, or null if the resource could not be found

getSystemResource

public static URL getSystemResource(String name)
Finds the system resource with the given name. System resources are those loaded from the system class path.
Parameters:
name - the resource name
Returns:
a URL for reading the resource, or null if the resource could not be found

getSystemResources

public static Enumeration getSystemResources(String name)
Returns an enumeration of URLs specifying all of the system resources with the specified name. System resources are those loaded from the system class path.
Parameters:
name - the resource name
Returns:
an enumeration of resource URLs

getResourceAsStream

public InputStream getResourceAsStream(String name)
Returns an input stream for reading the specified resource.
Parameters:
name - the resource name
Returns:
an input stream for reading the resource, or null if the resource could not be found

getSystemResourceAsStream

public static InputStream getSystemResourceAsStream(String name)
Returns an input stream for reading the specified system resource. System resources are those loaded from the system class path.
Parameters:
name - the resource name
Returns:
an input stream for reading the resource, or null if the resource could not be found

getBaseClassLoader

public static ClassLoader getBaseClassLoader()
Returns the base class loader for delegation. This is the default delegation parent for new ClassLoader instances, and is usually the ClassLoader that was used to start the application.
Returns:
the base ClassLoader for delegation, or null if none
Throws:
SecurityException - if the caller does not have RuntimePermission("getClassLoader") permission

definePackage

protected Package definePackage(String name,
                                String specTitle,
                                String specVersion,
                                String specVendor,
                                String implTitle,
                                String implVersion,
                                String implVendor,
                                URL sealBase) throws IllegalArgumentException
Defines a package by name in this ClassLoader. This allows class loaders to define the packages for their classes. Packages must be created before the class is defined, and package names must be unique within a class loader and cannot be redefined or changed once created.
Parameters:
name - the package name
specTitle - the specification title
specVersion - the specification version
specVendor - the specification vendor
implTitle - the implementation title
implVersion - the implementation version
implVendor - the implementation vendor
sealBase - if specified, then this package is sealed with respect to the given base URL
Throws:
IllegalArgumentException - if package name duplicates and existing package either in this class loader or the parent class loader

getPackage

protected Package getPackage(String name)
Returns the Package defined in this ClassLoader for the given name.
Parameters:
name - the package name
Returns:
the Package corresponding to the given name, or null if not found

getPackages

protected Package[] getPackages()
Returns all of the Packages defined in this ClassLoader.

Contents | Package | Class | Tree | Deprecated | Index | Help Java 1.2 Beta 3
PREV | NEXT SHOW LISTS | HIDE LISTS

Submit a bug or feature
Submit comments/suggestions about new javadoc look.
Java is a trademark or registered trademark of Sun Microsystems, Inc. in the US and other countries.
Copyright 1993-1998 Sun Microsystems, Inc. 901 San Antonio Road, Palo Alto, California, 94303, U.S.A. All Rights Reserved.