Hongqing Liu | 7189829 | 2014-10-15 13:31:32 +0800 | [diff] [blame] | 1 | <html><head><META http-equiv="Content-Type" content="text/html; charset=iso-8859-1"><title>Apache Tomcat 6.0 (6.0.41) - Class Loader HOW-TO</title><meta name="author" content="Craig R. McClanahan"><meta name="author" content="Yoav Shapira"><style type="text/css" media="print">
|
Hongqing Liu | fd5ee81 | 2014-05-10 16:32:51 +0800 | [diff] [blame] | 2 | .noPrint {display: none;}
|
| 3 | td#mainBody {width: 100%;}
|
| 4 | </style></head><body bgcolor="#ffffff" text="#000000" link="#525D76" alink="#525D76" vlink="#525D76"><table border="0" width="100%" cellspacing="0"><!--PAGE HEADER--><tr><td><!--PROJECT LOGO--><a href="http://tomcat.apache.org/"><img src="./images/tomcat.gif" align="right" alt="
|
| 5 | The Apache Tomcat Servlet/JSP Container
|
Hongqing Liu | 7189829 | 2014-10-15 13:31:32 +0800 | [diff] [blame] | 6 | " border="0"></a></td><td><h1><font face="arial,helvetica,sanserif">Apache Tomcat 6.0</font></h1><font face="arial,helvetica,sanserif">Version 6.0.41, May 19 2014</font></td><td><!--APACHE LOGO--><a href="http://www.apache.org/"><img src="./images/asf-logo.gif" align="right" alt="Apache Logo" border="0"></a></td></tr></table><table border="0" width="100%" cellspacing="4"><!--HEADER SEPARATOR--><tr><td colspan="2"><hr noshade="noshade" size="1"></td></tr><tr><!--LEFT SIDE NAVIGATION--><td width="20%" valign="top" nowrap="nowrap" class="noPrint"><p><strong>Links</strong></p><ul><li><a href="index.html">Docs Home</a></li><li><a href="http://wiki.apache.org/tomcat/FAQ">FAQ</a></li></ul><p><strong>User Guide</strong></p><ul><li><a href="introduction.html">1) Introduction</a></li><li><a href="setup.html">2) Setup</a></li><li><a href="appdev/index.html">3) First webapp</a></li><li><a href="deployer-howto.html">4) Deployer</a></li><li><a href="manager-howto.html">5) Manager</a></li><li><a href="realm-howto.html">6) Realms and AAA</a></li><li><a href="security-manager-howto.html">7) Security Manager</a></li><li><a href="jndi-resources-howto.html">8) JNDI Resources</a></li><li><a href="jndi-datasource-examples-howto.html">9) JDBC DataSources</a></li><li><a href="class-loader-howto.html">10) Classloading</a></li><li><a href="jasper-howto.html">11) JSPs</a></li><li><a href="ssl-howto.html">12) SSL</a></li><li><a href="ssi-howto.html">13) SSI</a></li><li><a href="cgi-howto.html">14) CGI</a></li><li><a href="proxy-howto.html">15) Proxy Support</a></li><li><a href="mbeans-descriptor-howto.html">16) MBean Descriptor</a></li><li><a href="default-servlet.html">17) Default Servlet</a></li><li><a href="cluster-howto.html">18) Clustering</a></li><li><a href="balancer-howto.html">19) Load Balancer</a></li><li><a href="connectors.html">20) Connectors</a></li><li><a href="monitoring.html">21) Monitoring and Management</a></li><li><a href="logging.html">22) Logging</a></li><li><a href="apr.html">23) APR/Native</a></li><li><a href="virtual-hosting-howto.html">24) Virtual Hosting</a></li><li><a href="aio.html">25) Advanced IO</a></li><li><a href="extras.html">26) Additional Components</a></li><li><a href="maven-jars.html">27) Mavenized</a></li></ul><p><strong>Reference</strong></p><ul><li><a href="RELEASE-NOTES.txt">Release Notes</a></li><li><a href="config/index.html">Configuration</a></li><li><a href="api/index.html">Javadocs</a></li><li><a href="http://tomcat.apache.org/connectors-doc/">JK 1.2 Documentation</a></li></ul><p><strong>Apache Tomcat Development</strong></p><ul><li><a href="building.html">Building</a></li><li><a href="changelog.html">Changelog</a></li><li><a href="http://wiki.apache.org/tomcat/TomcatVersions">Status</a></li><li><a href="developers.html">Developers</a></li><li><a href="architecture/index.html">Architecture</a></li><li><a href="funcspecs/index.html">Functional Specs.</a></li></ul></td><!--RIGHT SIDE MAIN BODY--><td width="80%" valign="top" align="left" id="mainBody"><h1>Apache Tomcat 6.0</h1><h2>Class Loader HOW-TO</h2><table border="0" cellspacing="0" cellpadding="2"><tr><td bgcolor="#525D76"><font color="#ffffff" face="arial,helvetica.sanserif"><a name="Table of Contents"><!--()--></a><a name="Table_of_Contents"><strong>Table of Contents</strong></a></font></td></tr><tr><td><blockquote>
|
Hongqing Liu | fd5ee81 | 2014-05-10 16:32:51 +0800 | [diff] [blame] | 7 | <ul><li><a href="#Overview">Overview</a></li><li><a href="#Class_Loader_Definitions">Class Loader Definitions</a></li><li><a href="#XML_Parsers_and_Java">XML Parsers and Java</a></li><li><a href="#Running_under_a_security_manager">Running under a security manager</a></li></ul>
|
| 8 | </blockquote></td></tr></table><table border="0" cellspacing="0" cellpadding="2"><tr><td bgcolor="#525D76"><font color="#ffffff" face="arial,helvetica.sanserif"><a name="Overview"><strong>Overview</strong></a></font></td></tr><tr><td><blockquote>
|
| 9 |
|
| 10 | <p>Like many server applications, Tomcat installs a variety of class loaders
|
| 11 | (that is, classes that implement <code>java.lang.ClassLoader</code>) to allow
|
| 12 | different portions of the container, and the web applications running on the
|
| 13 | container, to have access to different repositories of available classes and
|
| 14 | resources. This mechanism is used to provide the functionality defined in the
|
| 15 | Servlet Specification, version 2.4 — in particular, Sections 9.4
|
| 16 | and 9.6.</p>
|
| 17 |
|
| 18 | <p>In a Java environment, class loaders are
|
| 19 | arranged in a parent-child tree. Normally, when a class loader is asked to
|
| 20 | load a particular class or resource, it delegates the request to a parent
|
| 21 | class loader first, and then looks in its own repositories only if the parent
|
| 22 | class loader(s) cannot find the requested class or resource. Note, that the
|
| 23 | model for web application class loaders <em>differs</em> slightly from this,
|
| 24 | as discussed below, but the main principles are the same.</p>
|
| 25 |
|
| 26 | <p>When Tomcat is started, it creates a set of class loaders that are
|
| 27 | organized into the following parent-child relationships, where the parent
|
| 28 | class loader is above the child class loader:</p>
|
| 29 |
|
| 30 | <div align="left"><table cellspacing="4" cellpadding="0" border="0"><tr><td bgcolor="#023264" width="1" height="1"><img src="./images/void.gif" alt="" width="1" height="1" vspace="0" hspace="0" border="0"></td><td bgcolor="#023264" height="1"><img src="./images/void.gif" alt="" width="1" height="1" vspace="0" hspace="0" border="0"></td><td bgcolor="#023264" width="1" height="1"><img src="./images/void.gif" alt="" width="1" height="1" vspace="0" hspace="0" border="0"></td></tr><tr><td bgcolor="#023264" width="1"><img src="./images/void.gif" alt="" width="1" height="1" vspace="0" hspace="0" border="0"></td><td bgcolor="#ffffff" height="1"><pre>
|
| 31 | Bootstrap
|
| 32 | |
|
| 33 | System
|
| 34 | |
|
| 35 | Common
|
| 36 | / \
|
| 37 | Webapp1 Webapp2 ...
|
| 38 | </pre></td><td bgcolor="#023264" width="1"><img src="./images/void.gif" alt="" width="1" height="1" vspace="0" hspace="0" border="0"></td></tr><tr><td bgcolor="#023264" width="1" height="1"><img src="./images/void.gif" alt="" width="1" height="1" vspace="0" hspace="0" border="0"></td><td bgcolor="#023264" height="1"><img src="./images/void.gif" alt="" width="1" height="1" vspace="0" hspace="0" border="0"></td><td bgcolor="#023264" width="1" height="1"><img src="./images/void.gif" alt="" width="1" height="1" vspace="0" hspace="0" border="0"></td></tr></table></div>
|
| 39 |
|
| 40 | <p>The characteristics of each of these class loaders, including the source
|
| 41 | of classes and resources that they make visible, are discussed in detail in
|
| 42 | the following section.</p>
|
| 43 |
|
| 44 | </blockquote></td></tr></table><table border="0" cellspacing="0" cellpadding="2"><tr><td bgcolor="#525D76"><font color="#ffffff" face="arial,helvetica.sanserif"><a name="Class Loader Definitions"><!--()--></a><a name="Class_Loader_Definitions"><strong>Class Loader Definitions</strong></a></font></td></tr><tr><td><blockquote>
|
| 45 |
|
| 46 | <p>As indicated in the diagram above, Tomcat 6 creates the following class
|
| 47 | loaders as it is initialized:</p>
|
| 48 | <ul>
|
| 49 | <li><p><strong>Bootstrap</strong> — This class loader contains the basic
|
| 50 | runtime classes provided by the Java Virtual Machine, plus any classes from
|
| 51 | JAR files present in the System Extensions directory
|
| 52 | (<code>$JAVA_HOME/jre/lib/ext</code>). <em>Note</em>: some JVMs may
|
| 53 | implement this as more than one class loader, or it may not be visible
|
| 54 | (as a class loader) at all.</p></li>
|
| 55 | <li><p><strong>System</strong> — This class loader is normally initialized
|
| 56 | from the contents of the <code>CLASSPATH</code> environment variable. All
|
| 57 | such classes are visible to both Tomcat internal classes, and to web
|
| 58 | applications. However, the standard Tomcat startup scripts
|
| 59 | (<code>$CATALINA_HOME/bin/catalina.sh</code> or
|
| 60 | <code>%CATALINA_HOME%\bin\catalina.bat</code>) totally ignore the contents
|
| 61 | of the <code>CLASSPATH</code> environment variable itself, and instead
|
| 62 | build the System class loader from the following repositories:
|
| 63 | </p>
|
| 64 | <ul>
|
| 65 | <li><p><em>$CATALINA_HOME/bin/bootstrap.jar</em> — Contains the
|
| 66 | main() method that is used to initialize the Tomcat server, and the
|
| 67 | class loader implementation classes it depends on.</p></li>
|
| 68 | <li><p><em>$CATALINA_BASE/bin/tomcat-juli.jar</em> and
|
| 69 | <em>$CATALINA_HOME/bin/tomcat-juli.jar</em> — Logging
|
| 70 | implementation classes. These include enhancement classes to
|
| 71 | <code>java.util.logging</code> API, known as Tomcat JULI,
|
| 72 | and a package-renamed copy of Apache Commons Logging library
|
| 73 | used internally by Tomcat.
|
| 74 | See <a href="logging.html">logging documentation</a> for more
|
| 75 | details.</p></li>
|
| 76 | <li><p><em>$CATALINA_HOME/bin/commons-daemon.jar</em> — The classes
|
| 77 | from <a href="http://commons.apache.org/daemon/">Apache Commons
|
| 78 | Daemon</a> project.</p></li>
|
| 79 | </ul>
|
| 80 | <p>The <em>tomcat-juli.jar</em> and <em>commons-daemon.jar</em> JARs in
|
| 81 | <em>$CATALINA_HOME/bin</em> are not present in the <code>CLASSPATH</code>
|
| 82 | built by <code>catalina.bat</code>|<code>.sh</code> scripts, but are
|
| 83 | referenced from the manifest file of <em>bootstrap.jar</em>.
|
| 84 | </p>
|
| 85 | <p>If <em>$CATALINA_BASE</em> and <em>$CATALINA_HOME</em> do differ and
|
| 86 | <em>$CATALINA_BASE/bin/tomcat-juli.jar</em> does exist, the startup scripts
|
| 87 | will add it to <code>CLASSPATH</code> before <em>bootstrap.jar</em>, so
|
| 88 | that Java will look into <em>$CATALINA_BASE/bin/tomcat-juli.jar</em> for
|
| 89 | classes before it will look into <em>$CATALINA_HOME/bin/tomcat-juli.jar</em>
|
| 90 | referenced by <em>bootstrap.jar</em>. It should work in most cases but,
|
| 91 | if you are using such configuration, it might be recommended to remove
|
| 92 | <em>tomcat-juli.jar</em> from <em>$CATALINA_HOME/bin</em> so that only
|
| 93 | one copy of the file is present on the classpath. The next version of
|
| 94 | Tomcat, Tomcat 7, takes different approach here.
|
| 95 | </p></li>
|
| 96 | <li><p><strong>Common</strong> — This class loader contains additional
|
| 97 | classes that are made visible to both Tomcat internal classes and to all
|
| 98 | web applications.</p>
|
| 99 | <p>Normally, application classes should <strong>NOT</strong>
|
| 100 | be placed here. The locations searched by this class loader are defined by
|
| 101 | the <code>common.loader</code> property in
|
| 102 | $CATALINA_BASE/conf/catalina.properties. The default setting will search the
|
| 103 | following locations in the order they are listed:</p>
|
| 104 | <ul>
|
| 105 | <li>unpacked classes and resources in <code>$CATALINA_BASE/lib</code></li>
|
| 106 | <li>JAR files in <code>$CATALINA_BASE/lib</code></li>
|
| 107 | <li>unpacked classes and resources in <code>$CATALINA_HOME/lib</code></li>
|
| 108 | <li>JAR files in <code>$CATALINA_HOME/lib</code></li>
|
| 109 | </ul>
|
| 110 | <p>By default, this includes the following:</p>
|
| 111 | <ul>
|
| 112 | <li><em>annotations-api.jar</em> — JavaEE annotations classes.</li>
|
| 113 | <li><em>catalina.jar</em> — Implementation of the Catalina servlet
|
| 114 | container portion of Tomcat.</li>
|
| 115 | <li><em>catalina-ant.jar</em> — Tomcat Catalina Ant tasks.</li>
|
| 116 | <li><em>catalina-ha.jar</em> — High availability package.</li>
|
| 117 | <li><em>catalina-tribes.jar</em> — Group communication package.</li>
|
| 118 | <li><em>ecj-*.jar</em> — Eclipse JDT Java compiler.</li>
|
| 119 | <li><em>el-api.jar</em> — EL 2.1 API.</li>
|
| 120 | <li><em>jasper.jar</em> — Tomcat Jasper JSP Compiler and Runtime.</li>
|
| 121 | <li><em>jasper-el.jar</em> — Tomcat Jasper EL implementation.</li>
|
| 122 | <li><em>jsp-api.jar</em> — JSP 2.1 API.</li>
|
| 123 | <li><em>servlet-api.jar</em> — Servlet 2.5 API.</li>
|
| 124 | <li><em>tomcat-coyote.jar</em> — Tomcat connectors and utility classes.</li>
|
| 125 | <li><em>tomcat-dbcp.jar</em> — Database connection pool
|
| 126 | implementation based on package-renamed copy of Apache Commons Pool
|
| 127 | and Apache Commons DBCP.</li>
|
| 128 | <li><em>tomcat-i18n-**.jar</em> — Optional JARs containing resource bundles
|
| 129 | for other languages. As default bundles are also included in each
|
| 130 | individual JAR, they can be safely removed if no internationalization
|
| 131 | of messages is needed.</li>
|
| 132 | </ul></li>
|
| 133 | <li><p><strong>WebappX</strong> — A class loader is created for each web
|
| 134 | application that is deployed in a single Tomcat instance. All unpacked
|
| 135 | classes and resources in the <code>/WEB-INF/classes</code> directory of
|
| 136 | your web application, plus classes and resources in JAR files
|
| 137 | under the <code>/WEB-INF/lib</code> directory of your web application,
|
| 138 | are made visible to this web application, but not to other ones.</p></li>
|
| 139 | </ul>
|
| 140 |
|
| 141 | <p>As mentioned above, the web application class loader diverges from the
|
| 142 | default Java 2 delegation model (in accordance with the recommendations in the
|
| 143 | Servlet Specification, version 2.4, section 9.7.2 Web Application Classloader).
|
| 144 | When a request to load a
|
| 145 | class from the web application's <em>WebappX</em> class loader is processed,
|
| 146 | this class loader will look in the local repositories <strong>first</strong>,
|
| 147 | instead of delegating before looking. There are exceptions. Classes which are
|
| 148 | part of the JRE base classes cannot be overriden. For some classes (such as
|
| 149 | the XML parser components in J2SE 1.4+), the J2SE 1.4 endorsed feature can be
|
| 150 | used.
|
| 151 | Last, any JAR file that contains Servlet API classes will be explicitly
|
| 152 | ignored by the classloader — Do not include such JARs in your web
|
| 153 | application.
|
| 154 | All other class loaders in Tomcat 6 follow the usual delegation pattern.</p>
|
| 155 |
|
| 156 | <p>Therefore, from the perspective of a web application, class or resource
|
| 157 | loading looks in the following repositories, in this order:</p>
|
| 158 | <ul>
|
| 159 | <li>Bootstrap classes of your JVM</li>
|
| 160 | <li>System class loader classes (described above)</li>
|
| 161 | <li><em>/WEB-INF/classes</em> of your web application</li>
|
| 162 | <li><em>/WEB-INF/lib/*.jar</em> of your web application</li>
|
| 163 | <li>Common class loader classes (described above)</li>
|
| 164 | </ul>
|
| 165 |
|
| 166 | </blockquote></td></tr></table><table border="0" cellspacing="0" cellpadding="2"><tr><td bgcolor="#525D76"><font color="#ffffff" face="arial,helvetica.sanserif"><a name="XML Parsers and Java"><!--()--></a><a name="XML_Parsers_and_Java"><strong>XML Parsers and Java</strong></a></font></td></tr><tr><td><blockquote>
|
| 167 |
|
| 168 | <p>Starting with Java 1.4 a copy of JAXP APIs and an XML parser are packed
|
| 169 | inside the JRE. This has impacts on applications that wish to use their own
|
| 170 | XML parser.</p>
|
| 171 |
|
| 172 | <p>In old versions of Tomcat, you could simply replace the XML parser
|
| 173 | in the Tomcat libraries directory to change the parser
|
| 174 | used by all web applications. However, this technique will not be effective
|
| 175 | when you are running modern versions of Java, because the usual class loader
|
| 176 | delegation process will always choose the implementation inside the JDK in
|
| 177 | preference to this one.</p>
|
| 178 |
|
| 179 | <p>Java supports a mechanism called the "Endorsed Standards Override
|
| 180 | Mechanism" to allow replacement of APIs created outside of the JCP
|
| 181 | (i.e. DOM and SAX from W3C). It can also be used to update the XML parser
|
| 182 | implementation. For more information, see:
|
| 183 | <a href="http://docs.oracle.com/javase/1.5.0/docs/guide/standards/index.html">
|
| 184 | http://docs.oracle.com/javase/1.5.0/docs/guide/standards/index.html</a>.</p>
|
| 185 |
|
| 186 | <p>Tomcat utilizes this mechanism by including the system property setting
|
| 187 | <code>-Djava.endorsed.dirs=$JAVA_ENDORSED_DIRS</code> in the
|
| 188 | command line that starts the container. The default value of this option is
|
| 189 | <em>$CATALINA_HOME/endorsed</em>. This <em>endorsed</em> directory is not
|
| 190 | created by default.</p>
|
| 191 |
|
| 192 | </blockquote></td></tr></table><table border="0" cellspacing="0" cellpadding="2"><tr><td bgcolor="#525D76"><font color="#ffffff" face="arial,helvetica.sanserif"><a name="Running under a security manager"><!--()--></a><a name="Running_under_a_security_manager"><strong>Running under a security manager</strong></a></font></td></tr><tr><td><blockquote>
|
| 193 |
|
| 194 | <p>When running under a security manager the locations from which classes
|
| 195 | are permitted to be loaded will also depend on the contents of your policy
|
| 196 | file. See <a href="security-manager-howto.html">Security Manager HOW-TO</a>
|
| 197 | for further information.</p>
|
| 198 |
|
| 199 | </blockquote></td></tr></table></td></tr><!--FOOTER SEPARATOR--><tr><td colspan="2"><hr noshade="noshade" size="1"></td></tr><!--PAGE FOOTER--><tr><td colspan="2"><div align="center"><font color="#525D76" size="-1"><em>
|
| 200 | Copyright © 1999-2014, Apache Software Foundation
|
| 201 | </em></font></div></td></tr></table></body></html> |