All Packages Class Hierarchy This Package Previous Next Index
Object | +----java.lang.ClassLoader
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 { 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; } }
Class
.
protected ClassLoader()
If there is a security manager, its
checkCreateClassLoader
method is called. This may
result in a security exception.
protected final java.lang.Class defineClass(byte data, int offset, int length)
Class
.
Before the Class can be used it must be resolved. This
method is deprecated in favor of the version that takes a
"name" as a first argument, and is more secure.
Class
.
Class
data.
Class
data.
Class
object that was created from the data.
protected final java.lang.Class defineClass(String name, byte data, int offset, int length)
Class
.
Class
data.
Class
data.
Class
object that was created from the data.
protected final java.lang.Class findLoadedClass(String name)
protected final java.lang.Class findSystemClass(String name) throws ClassNotFoundException
A system class is a class loaded from the local file system in a platform- dependent way. It has no class loader.
class
.
public java.net.URL getResource(String name)
The class loader can choose what to do to locate the resource.
public java.io.InputStream getResourceAsStream(String name)
The class loader can choose what to do to locate the resource.
public static final java.net.URL getSystemResource(String name)
The resource name may be any system resource (e.g. follows CLASSPATH order).
public static final java.io.InputStream getSystemResourceAsStream(String name)
The resource name may be any system resource (e.g. follows CLASSPATH order).
public java.lang.Class loadClass(String name) throws ClassNotFoundException
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.
Class loaders should use a hashtable or other cache to avoid defining classes with the same name multiple times.
Class
.
Class
, or null
if it was not found.
protected abstract java.lang.Class loadClass(String name, boolean resolve) throws ClassNotFoundException
If the resolve
flag is true, the method should call
the resolveClass
method on the resulting class object.
As an abstract method, loadClass() must be defined in a subclass of ClassLoader. By using a Hashtable, you can avoid loading the same Class more than once.
protected final void resolveClass(Class c)
loadClass
if the resolve flag is
true
.
Class
instance to be resolved.
protected final java.lang.Class setLoadedClass(Class cl)
protected final void setSigners(Class cl, Object[] signers)
All Packages Class Hierarchy This Package Previous Next Index