All Packages Class Hierarchy This Package Previous Next Index
java.lang.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 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 . . . } }
SecurityException
if the calling thread is not allowed to access the package specified
by the argument.
Class
.
Deprecated.
Class
.
null
if no parent class loader was specified and loadClass
should delegate to the system class loader instead.
protected ClassLoader(ClassLoader parent)
If there is a security manager, its checkCreateClassLoader
method is called. This may result in a security exception.
protected ClassLoader()
If there is a security manager, its checkCreateClassLoader
method is called. This may result in a security exception.
public Class loadClass(String name) throws ClassNotFoundException
resolveClass
method will be called on the resulting Class
object.
Class
objectprotected Class loadClass(String name, boolean resolve) throws ClassNotFoundException
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:
findLoadedClass
to check if the class has
already been loaded.
loadClass
method of the parent class
loader to load the class, or findSystemClass
if no parent class loader was specified.
findLocalClass
method to load the class
locally.
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
.
true
then resolve the class
Class
objectprotected void checkPackageAccess(String name) throws SecurityException
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)); }
protected Class findLocalClass(String name)
loadClass
method after checking the parent class loader for the requested class.
The default implementation of this method returns null
.
Class
object, or null
if the class could not be foundprotected final Class defineClass(byte[] b, int off, int len) throws ClassFormatError
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.
Class
object that was created from the
specified class dataprotected final Class defineClass(String name, byte[] b, int off, int len) throws ClassFormatError
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.
null
if not known, using '.' and not '/' as the separator
and without a trailing ".class" suffix.
Class
object that was created from the
specified class dataprotected final void resolveClass(Class c)
loadClass
if the resolve flag is
true
.
protected final Class findSystemClass(String name) throws ClassNotFoundException
A system class is a class loaded from the local file system in a platform-dependent manner, and has no associated class loader.
public ClassLoader getParent()
null
if no parent class loader was specified and loadClass
should delegate to the system class loader instead.
null
if noneprotected final void setSigners(Class c, Object[] signers)
Class
object
protected final Class findLoadedClass(String name)
Class object, or null
if
the class has not been loaded
public URL getResource(String name)
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.
null
if
the resource could not be foundpublic final Enumeration getResources(String name) throws IOException
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.
public Enumeration getLocalResources(String name) throws IOException
null
if the resource could not be foundpublic URL getLocalResource(String name)
null
if the resource could not be foundpublic static URL getSystemResource(String name)
null
if
the resource could not be foundpublic InputStream getResourceAsStream(String name)
null
if the resource could not be foundpublic static InputStream getSystemResourceAsStream(String name)
null
if the resource could not be foundpublic static ClassLoader currentClassLoader()
null
if there is no occurrence on the
stack of a method from a class defined using a class loader.protected Package definePackage(String name, String specTitle, String specVersion, String specVendor, String implTitle, String implVersion, String implVendor, URL sealBase) throws IllegalArgumentException
protected Package getPackage(String name)
protected Package[] getPackages()
All Packages Class Hierarchy This Package Previous Next Index