All Packages  Class Hierarchy  This Package  Previous  Next  Index  

Class java.lang.ClassLoader

java.lang.Object
    |
    +----java.lang.ClassLoader

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, loadClass, resolveClass

Constructor Index

 o ClassLoader()
Creates a new class loader with no parent class loader.
 o ClassLoader(ClassLoader)
Creates a new class loader using the specified parent class loader for delegation.

Method Index

 o checkPackageAccess(String)
This method can be overridden to throw a SecurityException if the calling thread is not allowed to access the package specified by the argument.
 o currentClassLoader()
Find the current class loader.
 o defineClass(byte[], int, int)
Converts an array of bytes into an instance of class Class. Deprecated.
 o defineClass(String, byte[], int, int)
Converts an array of bytes into an instance of class Class.
 o definePackage(String, String, String, String, String, String, String, URL)
Defines a package by name in this ClassLoader.
 o findLoadedClass(String)
Finds the class with the given name if it had been previously loaded through this class loader.
 o findLocalClass(String)
Finds the specified class locally.
 o findSystemClass(String)
Finds the system class with the specified name, loading it in if necessary.
 o getLocalResource(String)
Finds the local resource with the given name.
 o getLocalResources(String)
Finds the local resource with the given name.
 o getPackage(String)
 o getPackages()
 o getParent()
Returns the parent class loader for delegation, or null if no parent class loader was specified and loadClass should delegate to the system class loader instead.
 o getResource(String)
Finds the resource with the given name.
 o getResourceAsStream(String)
Returns an input stream for reading the specified resource.
 o getResources(String)
Finds all the resources with the given name.
 o getSystemResource(String)
Finds the system resource with the given name.
 o getSystemResourceAsStream(String)
Returns an input stream for reading the specified system resource.
 o loadClass(String)
Loads the class with the specified name.
 o loadClass(String, boolean)
Loads the class with the specified name.
 o resolveClass(Class)
Resolves the class so that an instance of the class can be created, or so that one of its methods can be called.
 o setSigners(Class, Object[])
Sets the signers of a class.

Constructors

 o 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
 o ClassLoader
protected ClassLoader()
Creates a new class loader with no 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

Methods

 o 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
 o 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 load 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
 o 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
 o findLocalClass
protected Class findLocalClass(String name)
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 of this method returns null.

Parameters:
name - the name of the class
Returns:
the resulting Class object, or null if the class could not be found
 o 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, resolveClass
 o 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 is deprecated in favor of the version that takes the class name as its first argument, and is more secure.

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, resolveClass
 o 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
 o 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
 o getParent
public ClassLoader getParent()
Returns the parent class loader for delegation, or null if no parent class loader was specified and loadClass should delegate to the system class loader instead.

Returns:
the parent class loader, or null if none
 o 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
 o 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
 o 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
See Also:
getLocalResource
 o 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.
See Also:
getResource, getLocalResources
 o getLocalResources
public Enumeration getLocalResources(String name) throws IOException
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
 o 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
 o 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
 o 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
 o 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
 o currentClassLoader
public static ClassLoader currentClassLoader()
Find the current class loader. The runtime stack of callers of this method is examined to identify the ClassLoader that loaded the first non-system class.

Returns:
the class loader of the most recent occurrence on the stack of a method from a class defined using a class loader; returns null if there is no occurrence on the stack of a method from a class defined using a class loader.
 o 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
 o getPackage
protected Package getPackage(String name)
 o getPackages
protected Package[] getPackages()

All Packages  Class Hierarchy  This Package  Previous  Next  Index  

Submit a bug or feature