📄 classloader.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Frameset//EN""http://www.w3.org/TR/REC-html40/frameset.dtd"><!--NewPage--><HTML><HEAD><!-- Generated by javadoc on Thu Apr 27 23:35:01 PDT 2000 --><TITLE>Java 2 Platform SE v1.3: Class ClassLoader</TITLE><LINK REL ="stylesheet" TYPE="text/css" HREF="../../stylesheet.css" TITLE="Style"></HEAD><BODY BGCOLOR="white"><!-- ========== START OF NAVBAR ========== --><A NAME="navbar_top"><!-- --></A><TABLE BORDER="0" WIDTH="100%" CELLPADDING="1" CELLSPACING="0"><TR><TD COLSPAN=2 BGCOLOR="#EEEEFF" CLASS="NavBarCell1"><A NAME="navbar_top_firstrow"><!-- --></A><TABLE BORDER="0" CELLPADDING="0" CELLSPACING="3"> <TR ALIGN="center" VALIGN="top"> <TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1"> <A HREF="../../overview-summary.html"><FONT CLASS="NavBarFont1"><B>Overview</B></FONT></A> </TD> <TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1"> <A HREF="package-summary.html"><FONT CLASS="NavBarFont1"><B>Package</B></FONT></A> </TD> <TD BGCOLOR="#FFFFFF" CLASS="NavBarCell1Rev"> <FONT CLASS="NavBarFont1Rev"><B>Class</B></FONT> </TD> <TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1"> <A HREF="class-use/ClassLoader.html"><FONT CLASS="NavBarFont1"><B>Use</B></FONT></A> </TD> <TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1"> <A HREF="package-tree.html"><FONT CLASS="NavBarFont1"><B>Tree</B></FONT></A> </TD> <TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1"> <A HREF="../../deprecated-list.html"><FONT CLASS="NavBarFont1"><B>Deprecated</B></FONT></A> </TD> <TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1"> <A HREF="../../index-files/index-1.html"><FONT CLASS="NavBarFont1"><B>Index</B></FONT></A> </TD> <TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1"> <A HREF="../../help-doc.html"><FONT CLASS="NavBarFont1"><B>Help</B></FONT></A> </TD> </TR></TABLE></TD><TD ALIGN="right" VALIGN="top" ROWSPAN=3><EM><b>Java<sup><font size=-2>TM</font></sup> 2 Platform<br>Std. Ed. v1.3</b></EM></TD></TR><TR><TD BGCOLOR="white" CLASS="NavBarCell2"><FONT SIZE="-2"> <A HREF="../../java/lang/Class.html"><B>PREV CLASS</B></A> <A HREF="../../java/lang/Compiler.html"><B>NEXT CLASS</B></A></FONT></TD><TD BGCOLOR="white" CLASS="NavBarCell2"><FONT SIZE="-2"> <A HREF="../../index.html" TARGET="_top"><B>FRAMES</B></A> <A HREF="ClassLoader.html" TARGET="_top"><B>NO FRAMES</B></A></FONT></TD></TR><TR><TD VALIGN="top" CLASS="NavBarCell3"><FONT SIZE="-2"> SUMMARY: INNER | FIELD | <A HREF="#constructor_summary">CONSTR</A> | <A HREF="#method_summary">METHOD</A></FONT></TD><TD VALIGN="top" CLASS="NavBarCell3"><FONT SIZE="-2">DETAIL: FIELD | <A HREF="#constructor_detail">CONSTR</A> | <A HREF="#method_detail">METHOD</A></FONT></TD></TR></TABLE><!-- =========== END OF NAVBAR =========== --><HR><!-- ======== START OF CLASS DATA ======== --><H2><FONT SIZE="-1">java.lang</FONT><BR>Class ClassLoader</H2><PRE><A HREF="../../java/lang/Object.html">java.lang.Object</A> | +--<B>java.lang.ClassLoader</B></PRE><DL><DT><B>Direct Known Subclasses:</B> <DD><A HREF="../../java/security/SecureClassLoader.html">SecureClassLoader</A></DD></DL><HR><DL><DT>public abstract class <B>ClassLoader</B><DT>extends <A HREF="../../java/lang/Object.html">Object</A></DL><P>The class <code>ClassLoader</code> is an abstract class. A class loader is an object that is responsible for loading classes. Given the name of a class, it should attempt to locate or generate data that constitutes a definition for the class. A typical strategy is to transform the name into a file name and then read a "class file" of that name from a file system. <p> Every <code>Class</code> object contains a <A HREF="../../java/lang/Class.html#getClassLoader()"><CODE>reference</CODE></A> to the <code>ClassLoader</code> that defined it. <p> Class objects for array classes are not created by class loaders, but are created automatically as required by the Java runtime. The class loader for an array class, as returned by <A HREF="../../java/lang/Class.html#getClassLoader()"><CODE>Class.getClassLoader()</CODE></A> is the same as the class loader for its element type; if the element type is a primitive type, then the array class has no class loader. <p> Applications implement subclasses of <code>ClassLoader</code> in order to extend the manner in which the Java virtual machine dynamically loads classes. <p> Class loaders may typically be used by security managers to indicate security domains. <p> The <code>ClassLoader</code> class uses a delegation model to search for classes and resources. Each instance of <code>ClassLoader</code> has an associated parent class loader. When called upon to find a class or resource, a <code>ClassLoader</code> instance will delegate the search for the class or resource to its parent class loader before attempting to find the class or resource itself. The virtual machine's built-in class loader, called the bootstrap class loader, does not itself have a parent but may serve as the parent of a <code>ClassLoader</code> instance. <p> 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 <code>CLASSPATH</code> environment variable. <p> 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 <code>defineClass</code> converts an array of bytes into an instance of class <code>Class</code>. Instances of this newly defined class can be created using the <code>newInstance</code> method in class <code>Class</code>. <p> 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 <code>loadClass</code> method of the class loader that originally created the class. <p> For example, an application could create a network class loader to download class files from a server. Sample code might look like: <blockquote><pre> ClassLoader loader = new NetworkClassLoader(host, port); Object main = loader.loadClass("Main", true).newInstance(); . . . </pre></blockquote> <p> The network class loader subclass must define the methods <code>findClass</code> and <code>loadClassData</code> to load a class from the network. Once it has downloaded the bytes that make up the class, it should use the method <code>defineClass</code> to create a class instance. A sample implementation is: <p><hr><blockquote><pre> class NetworkClassLoader extends ClassLoader { String host; int port; public Class findClass(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 . . . } } </pre></blockquote><hr><P><DL><DT><B>Since: </B><DD>JDK1.0</DD><DT><B>See Also: </B><DD><A HREF="../../java/lang/Class.html"><CODE>Class</CODE></A>, <A HREF="../../java/lang/Class.html#newInstance()"><CODE>Class.newInstance()</CODE></A>, <A HREF="../../java/lang/ClassLoader.html#defineClass(byte[], int, int)"><CODE>defineClass(byte[], int, int)</CODE></A>, <A HREF="../../java/lang/ClassLoader.html#loadClass(java.lang.String, boolean)"><CODE>loadClass(java.lang.String, boolean)</CODE></A>, <A HREF="../../java/lang/ClassLoader.html#resolveClass(java.lang.Class)"><CODE>resolveClass(java.lang.Class)</CODE></A></DL><HR><P><!-- ======== INNER CLASS SUMMARY ======== --><!-- =========== FIELD SUMMARY =========== --><!-- ======== CONSTRUCTOR SUMMARY ======== --><A NAME="constructor_summary"><!-- --></A><TABLE BORDER="1" CELLPADDING="3" CELLSPACING="0" WIDTH="100%"><TR BGCOLOR="#CCCCFF" CLASS="TableHeadingColor"><TD COLSPAN=2><FONT SIZE="+2"><B>Constructor Summary</B></FONT></TD></TR><TR BGCOLOR="white" CLASS="TableRowColor"><TD ALIGN="right" VALIGN="top" WIDTH="1%"><FONT SIZE="-1"><CODE>protected </CODE></FONT></TD><TD><CODE><B><A HREF="../../java/lang/ClassLoader.html#ClassLoader()">ClassLoader</A></B>()</CODE><BR> Creates a new class loader using the <code>ClassLoader</code> returned by the method <code>getSystemClassLoader()</code> as the parent class loader.</TD></TR><TR BGCOLOR="white" CLASS="TableRowColor"><TD ALIGN="right" VALIGN="top" WIDTH="1%"><FONT SIZE="-1"><CODE>protected </CODE></FONT></TD><TD><CODE><B><A HREF="../../java/lang/ClassLoader.html#ClassLoader(java.lang.ClassLoader)">ClassLoader</A></B>(<A HREF="../../java/lang/ClassLoader.html">ClassLoader</A> parent)</CODE><BR> Creates a new class loader using the specified parent class loader for delegation.</TD></TR></TABLE> <!-- ========== METHOD SUMMARY =========== --><A NAME="method_summary"><!-- --></A><TABLE BORDER="1" CELLPADDING="3" CELLSPACING="0" WIDTH="100%"><TR BGCOLOR="#CCCCFF" CLASS="TableHeadingColor"><TD COLSPAN=2><FONT SIZE="+2"><B>Method Summary</B></FONT></TD></TR><TR BGCOLOR="white" CLASS="TableRowColor"><TD ALIGN="right" VALIGN="top" WIDTH="1%"><FONT SIZE="-1"><CODE>protected <A HREF="../../java/lang/Class.html">Class</A></CODE></FONT></TD><TD><CODE><B><A HREF="../../java/lang/ClassLoader.html#defineClass(byte[], int, int)">defineClass</A></B>(byte[] b, int off, int len)</CODE><BR> <B>Deprecated.</B> <I>Replaced by defineClass(java.lang.String, byte[], int, int)</I></TD></TR><TR BGCOLOR="white" CLASS="TableRowColor"><TD ALIGN="right" VALIGN="top" WIDTH="1%"><FONT SIZE="-1"><CODE>protected <A HREF="../../java/lang/Class.html">Class</A></CODE></FONT></TD><TD><CODE><B><A HREF="../../java/lang/ClassLoader.html#defineClass(java.lang.String, byte[], int, int)">defineClass</A></B>(<A HREF="../../java/lang/String.html">String</A> name, byte[] b, int off, int len)</CODE><BR> Converts an array of bytes into an instance of class <code>Class</code>.</TD></TR><TR BGCOLOR="white" CLASS="TableRowColor"><TD ALIGN="right" VALIGN="top" WIDTH="1%"><FONT SIZE="-1"><CODE>protected <A HREF="../../java/lang/Class.html">Class</A></CODE></FONT></TD><TD><CODE><B><A HREF="../../java/lang/ClassLoader.html#defineClass(java.lang.String, byte[], int, int, java.security.ProtectionDomain)">defineClass</A></B>(<A HREF="../../java/lang/String.html">String</A> name, byte[] b, int off, int len, <A HREF="../../java/security/ProtectionDomain.html">ProtectionDomain</A> protectionDomain)</CODE><BR> Converts an array of bytes into an instance of class Class, with an optional ProtectionDomain.</TD></TR><TR BGCOLOR="white" CLASS="TableRowColor"><TD ALIGN="right" VALIGN="top" WIDTH="1%"><FONT SIZE="-1"><CODE>protected <A HREF="../../java/lang/Package.html">Package</A></CODE></FONT></TD><TD><CODE><B><A HREF="../../java/lang/ClassLoader.html#definePackage(java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.net.URL)">definePackage</A></B>(<A HREF="../../java/lang/String.html">String</A> name, <A HREF="../../java/lang/String.html">String</A> specTitle, <A HREF="../../java/lang/String.html">String</A> specVersion, <A HREF="../../java/lang/String.html">String</A> specVendor, <A HREF="../../java/lang/String.html">String</A> implTitle, <A HREF="../../java/lang/String.html">String</A> implVersion, <A HREF="../../java/lang/String.html">String</A> implVendor, <A HREF="../../java/net/URL.html">URL</A> sealBase)</CODE><BR> Defines a package by name in this ClassLoader.</TD></TR><TR BGCOLOR="white" CLASS="TableRowColor"><TD ALIGN="right" VALIGN="top" WIDTH="1%"><FONT SIZE="-1"><CODE>protected <A HREF="../../java/lang/Class.html">Class</A></CODE></FONT></TD><TD><CODE><B><A HREF="../../java/lang/ClassLoader.html#findClass(java.lang.String)">findClass</A></B>(<A HREF="../../java/lang/String.html">String</A> name)</CODE><BR> Finds the specified class.</TD></TR><TR BGCOLOR="white" CLASS="TableRowColor">
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -