| [66] | 1 | <html><head><META http-equiv="Content-Type" content="text/html; charset=iso-8859-1"><title>Apache Tomcat 6.0 - Class Loader HOW-TO</title><meta value="Craig R. McClanahan" name="author"><meta value="craigmcc@apache.org" name="email"><meta value="Yoav Shapira" name="author"><meta value="yoavs@apache.org" name="email"></head><body vlink="#525D76" alink="#525D76" link="#525D76" text="#000000" bgcolor="#ffffff"><table cellspacing="0" width="100%" border="0"><!--PAGE HEADER--><tr><td><!--PROJECT LOGO--><a href="http://tomcat.apache.org/"><img border="0" alt=" | 
|---|
|  | 2 | The Apache Tomcat Servlet/JSP Container | 
|---|
|  | 3 | " align="right" src="./images/tomcat.gif"></a></td><td><font face="arial,helvetica,sanserif"><h1>Apache Tomcat 6.0</h1></font></td><td><!--APACHE LOGO--><a href="http://www.apache.org/"><img border="0" alt="Apache Logo" align="right" src="./images/asf-logo.gif"></a></td></tr></table><table cellspacing="4" width="100%" border="0"><!--HEADER SEPARATOR--><tr><td colspan="2"><hr size="1" noshade></td></tr><tr><!--LEFT SIDE NAVIGATION--><td nowrap="true" valign="top" width="20%"><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 align="left" valign="top" width="80%"><table cellspacing="4" width="100%" border="0"><tr><td valign="top" align="left"><h1>Apache Tomcat 6.0</h1><h2>Class Loader HOW-TO</h2></td><td nowrap="true" valign="top" align="right"><small><a href="printer/class-loader-howto.html"><img alt="Printer Friendly Version" border="0" src="./images/printer.gif"><br>print-friendly<br>version | 
|---|
|  | 4 | </a></small></td></tr></table><table cellpadding="2" cellspacing="0" border="0"><tr><td bgcolor="#525D76"><font face="arial,helvetica.sanserif" color="#ffffff"><a name="Overview"><strong>Overview</strong></a></font></td></tr><tr><td><blockquote> | 
|---|
|  | 5 |  | 
|---|
|  | 6 | <p>Like many server applications, Tomcat 6 installs a variety of class loaders | 
|---|
|  | 7 | (that is, classes that implement <code>java.lang.ClassLoader</code>) to allow | 
|---|
|  | 8 | different portions of the container, and the web applications running on the | 
|---|
|  | 9 | container, to have access to different repositories of available classes and | 
|---|
|  | 10 | resources.  This mechanism is used to provide the functionality defined in the | 
|---|
|  | 11 | Servlet Specification, version 2.4 -- in particular, Sections 9.4 and 9.6.</p> | 
|---|
|  | 12 |  | 
|---|
|  | 13 | <p>In a J2SE 2 (that is, J2SE 1.2 or later) environment, class loaders are | 
|---|
|  | 14 | arranged in a parent-child tree.  Normally, when a class loader is asked to | 
|---|
|  | 15 | load a particular class or resource, it delegates the request to a parent | 
|---|
|  | 16 | class loader first, and then looks in its own repositories only if the parent | 
|---|
|  | 17 | class loader(s) cannot find the requested class or resource.  The model for | 
|---|
|  | 18 | web application class loaders differs slightly from this, as discussed below, | 
|---|
|  | 19 | but the main principles are the same.</p> | 
|---|
|  | 20 |  | 
|---|
|  | 21 | <p>When Tomcat 6 is started, it creates a set of class loaders that are | 
|---|
|  | 22 | organized into the following parent-child relationships, where the parent | 
|---|
|  | 23 | class loader is above the child class loader:</p> | 
|---|
|  | 24 |  | 
|---|
|  | 25 | <div align="left"><table border="0" cellpadding="0" cellspacing="4"><tr><td height="1" width="1" bgcolor="#023264"><img border="0" hspace="0" vspace="0" height="1" width="1" src="./images/void.gif"></td><td height="1" bgcolor="#023264"><img border="0" hspace="0" vspace="0" height="1" width="1" src="./images/void.gif"></td><td height="1" width="1" bgcolor="#023264"><img border="0" hspace="0" vspace="0" height="1" width="1" src="./images/void.gif"></td></tr><tr><td width="1" bgcolor="#023264"><img border="0" hspace="0" vspace="0" height="1" width="1" src="./images/void.gif"></td><td height="1" bgcolor="#ffffff"><pre> | 
|---|
|  | 26 | Bootstrap | 
|---|
|  | 27 | | | 
|---|
|  | 28 | System | 
|---|
|  | 29 | | | 
|---|
|  | 30 | Common | 
|---|
|  | 31 | /     \ | 
|---|
|  | 32 | Webapp1   Webapp2 ... | 
|---|
|  | 33 | </pre></td><td width="1" bgcolor="#023264"><img border="0" hspace="0" vspace="0" height="1" width="1" src="./images/void.gif"></td></tr><tr><td height="1" width="1" bgcolor="#023264"><img border="0" hspace="0" vspace="0" height="1" width="1" src="./images/void.gif"></td><td height="1" bgcolor="#023264"><img border="0" hspace="0" vspace="0" height="1" width="1" src="./images/void.gif"></td><td height="1" width="1" bgcolor="#023264"><img border="0" hspace="0" vspace="0" height="1" width="1" src="./images/void.gif"></td></tr></table></div> | 
|---|
|  | 34 |  | 
|---|
|  | 35 | <p>The characteristics of each of these class loaders, including the source | 
|---|
|  | 36 | of classes and resources that they make visible, are discussed in detail in | 
|---|
|  | 37 | the following section.</p> | 
|---|
|  | 38 |  | 
|---|
|  | 39 | </blockquote></td></tr></table><table cellpadding="2" cellspacing="0" border="0"><tr><td bgcolor="#525D76"><font face="arial,helvetica.sanserif" color="#ffffff"><a name="Class Loader Definitions"><strong>Class Loader Definitions</strong></a></font></td></tr><tr><td><blockquote> | 
|---|
|  | 40 |  | 
|---|
|  | 41 | <p>As indicated in the diagram above, Tomcat 6 creates the following class | 
|---|
|  | 42 | loaders as it is initialized:</p> | 
|---|
|  | 43 | <ul> | 
|---|
|  | 44 | <li><strong>Bootstrap</strong> - This class loader contains the basic runtime | 
|---|
|  | 45 | classes provided by the Java Virtual Machine, plus any classes from JAR | 
|---|
|  | 46 | files present in the System Extensions directory | 
|---|
|  | 47 | (<code>$JAVA_HOME/jre/lib/ext</code>).  <em>NOTE</em> - Some JVMs may | 
|---|
|  | 48 | implement this as more than one class loader, or it may not be visible | 
|---|
|  | 49 | (as a class loader) at all.</li> | 
|---|
|  | 50 | <li><strong>System</strong> - This class loader is normally initialized from | 
|---|
|  | 51 | the contents of the <code>CLASSPATH</code> environment variable.  All such | 
|---|
|  | 52 | classes are visible to both Tomcat internal classes, and to web | 
|---|
|  | 53 | applications.  However, the standard Tomcat 6 startup scripts | 
|---|
|  | 54 | (<code>$CATALINA_HOME/bin/catalina.sh</code> or | 
|---|
|  | 55 | <code>%CATALINA_HOME%\bin\catalina.bat</code>) totally ignore the contents | 
|---|
|  | 56 | of the <code>CLASSPATH</code> environment variable itself, and instead | 
|---|
|  | 57 | build the System class loader from the following repositories: | 
|---|
|  | 58 | <ul> | 
|---|
|  | 59 | <li><em>$CATALINA_HOME/bin/bootstrap.jar</em> - Contains the main() method | 
|---|
|  | 60 | that is used to initialize the Tomcat 6 server, and the class loader | 
|---|
|  | 61 | implementation classes it depends on.</li> | 
|---|
|  | 62 | <li><em>$CATALINA_HOME/bin/tomcat-juli.jar</em> - Package renamed Jakarta commons | 
|---|
|  | 63 | logging API, and java.util.logging LogManager.</li> | 
|---|
|  | 64 | </ul></li> | 
|---|
|  | 65 | <li><strong>Common</strong> - This class loader contains additional classes | 
|---|
|  | 66 | that are made visible to both Tomcat internal classes and to all web | 
|---|
|  | 67 | applications.  Normally, application classes should <strong>NOT</strong> | 
|---|
|  | 68 | be placed here.  All unpacked classes and resources in | 
|---|
|  | 69 | <code>$CATALINA_HOME/lib</code>, as well as classes and | 
|---|
|  | 70 | resources in JAR files are made visible through this | 
|---|
|  | 71 | class loader.  By default, that includes the following: | 
|---|
|  | 72 | <ul> | 
|---|
|  | 73 | <li><em>annotations-api.jar</em> - JEE annotations classes.</li> | 
|---|
|  | 74 | <li><em>catalina.jar</em> - Implementation of the Catalina servlet | 
|---|
|  | 75 | container portion of Tomcat 6.</li> | 
|---|
|  | 76 | <li><em>catalina-ant.jar</em> - Tomcat Catalina Ant tasks.</li> | 
|---|
|  | 77 | <li><em>catalina-ha.jar</em> - High availability package.</li> | 
|---|
|  | 78 | <li><em>catalina-tribes.jar</em> - Group communication package.</li> | 
|---|
|  | 79 | <li><em>el-api.jar</em> - EL 2.1 API.</li> | 
|---|
|  | 80 | <li><em>jasper.jar</em> - Jasper 2 Compiler and Runtime.</li> | 
|---|
|  | 81 | <li><em>jasper-el.jar</em> - Jasper 2 EL implementation.</li> | 
|---|
|  | 82 | <li><em>jasper-jdt.jar</em> - Eclipse JDT 3.2 Java compiler.</li> | 
|---|
|  | 83 | <li><em>jsp-api.jar</em> - JSP 2.1 API.</li> | 
|---|
|  | 84 | <li><em>servlet-api.jar</em> - Servlet 2.5 API.</li> | 
|---|
|  | 85 | <li><em>tomcat-coyote.jar</em> - Tomcat connectors and utility classes.</li> | 
|---|
|  | 86 | <li><em>tomcat-dbcp.jar</em> - package renamed database connection | 
|---|
|  | 87 | pool based on Commons DBCP.</li> | 
|---|
|  | 88 | <li><em>tomcat-i18n-**.jar</em> - Optional JARs containing resource bundles | 
|---|
|  | 89 | for other languages. As default bundles are also included in each | 
|---|
|  | 90 | individual JAR, they can be safely removed if no internationalization | 
|---|
|  | 91 | of messages is needed.</li> | 
|---|
|  | 92 | </ul></li> | 
|---|
|  | 93 | <li><strong>WebappX</strong> - A class loader is created for each web | 
|---|
|  | 94 | application that is deployed in a single Tomcat 6 instance.  All unpacked | 
|---|
|  | 95 | classes and resources in the <code>/WEB-INF/classes</code> directory of | 
|---|
|  | 96 | your web application archive, plus classes and resources in JAR files | 
|---|
|  | 97 | under the <code>/WEB-INF/lib</code> directory of your web application | 
|---|
|  | 98 | archive, are made visible to the containing web application, but to | 
|---|
|  | 99 | no others.</li> | 
|---|
|  | 100 | </ul> | 
|---|
|  | 101 |  | 
|---|
|  | 102 | <p>As mentioned above, the web application class loader diverges from the | 
|---|
|  | 103 | default Java 2 delegation model (in accordance with the recommendations in the | 
|---|
|  | 104 | Servlet Specification, version 2.3, section 9.7.2 Web Application Classloader). | 
|---|
|  | 105 | When a request to load a | 
|---|
|  | 106 | class from the web application's <em>WebappX</em> class loader is processed, | 
|---|
|  | 107 | this class loader will look in the local repositories <strong>first</strong>, | 
|---|
|  | 108 | instead of delegating before looking.  There are exceptions. Classes which are | 
|---|
|  | 109 | part of the JRE base classes cannot be overriden. For some classes (such as | 
|---|
|  | 110 | the XML parser components in J2SE 1.4+), the J2SE 1.4 endorsed feature can be | 
|---|
|  | 111 | used. | 
|---|
|  | 112 | Last, any JAR containing servlet API classes will be ignored by the | 
|---|
|  | 113 | classloader. | 
|---|
|  | 114 | All other class loaders in Tomcat 6 follow the usual delegation pattern.</p> | 
|---|
|  | 115 |  | 
|---|
|  | 116 | <p>Therefore, from the perspective of a web application, class or resource | 
|---|
|  | 117 | loading looks in the following repositories, in this order:</p> | 
|---|
|  | 118 | <ul> | 
|---|
|  | 119 | <li>Bootstrap classes of your JVM</li> | 
|---|
|  | 120 | <li>System class loader classses (described above)</li> | 
|---|
|  | 121 | <li><em>/WEB-INF/classes</em> of your web application</li> | 
|---|
|  | 122 | <li><em>/WEB-INF/lib/*.jar</em> of your web application</li> | 
|---|
|  | 123 | <li><em>$CATALINA_HOME/lib</em></li> | 
|---|
|  | 124 | <li><em>$CATALINA_HOME/lib/*.jar</em></li> | 
|---|
|  | 125 | </ul> | 
|---|
|  | 126 |  | 
|---|
|  | 127 | </blockquote></td></tr></table><table cellpadding="2" cellspacing="0" border="0"><tr><td bgcolor="#525D76"><font face="arial,helvetica.sanserif" color="#ffffff"><a name="XML Parsers and JSE 5"><strong>XML Parsers and JSE 5</strong></a></font></td></tr><tr><td><blockquote> | 
|---|
|  | 128 |  | 
|---|
|  | 129 | <p>Among many other changes, the JSE 5 release packages the JAXP APIs, and | 
|---|
|  | 130 | a version of Xerces, inside the JRE.  This has impacts on applications that | 
|---|
|  | 131 | wish to use their own XML parser.</p> | 
|---|
|  | 132 |  | 
|---|
|  | 133 | <p>In previous versions of Tomcat, you could simply replace the XML parser | 
|---|
|  | 134 | in the <code>$CATALINA_HOME/common/lib</code> directory to change the parser | 
|---|
|  | 135 | used by all web applications.  However, this technique will not be effective | 
|---|
|  | 136 | when you are running on JSE 5, because the usual class loader delegation | 
|---|
|  | 137 | process will always choose the implementation inside the JDK in preference | 
|---|
|  | 138 | to this one.</p> | 
|---|
|  | 139 |  | 
|---|
|  | 140 | <p>JDK 1.5 supports a mechanism called the "Endorsed Standards Override | 
|---|
|  | 141 | Mechanism" to allow replacement of APIs created outside of the JCP (i.e. | 
|---|
|  | 142 | DOM and SAX from W3C).  It can also be used to update the XML parser | 
|---|
|  | 143 | implementation.  For more information, see: | 
|---|
|  | 144 | <a href="http://java.sun.com/j2se/1.5/docs/guide/standards/index.html"> | 
|---|
|  | 145 | http://java.sun.com/j2se/1.5/docs/guide/standards/index.html</a>.</p> | 
|---|
|  | 146 |  | 
|---|
|  | 147 | <p>Tomcat utilizes this mechanism by including the system property setting | 
|---|
|  | 148 | <code>-Djava.endorsed.dirs=$JAVA_ENDORSED_DIRS</code> in the | 
|---|
|  | 149 | command line that starts the container.</p> | 
|---|
|  | 150 |  | 
|---|
|  | 151 | </blockquote></td></tr></table><table cellpadding="2" cellspacing="0" border="0"><tr><td bgcolor="#525D76"><font face="arial,helvetica.sanserif" color="#ffffff"><a name="Running under a security manager"><strong>Running under a security manager</strong></a></font></td></tr><tr><td><blockquote> | 
|---|
|  | 152 |  | 
|---|
|  | 153 | <p>When running under a security manager the locations from which classes | 
|---|
|  | 154 | are permitted to be loaded will also depend on the contents of your policy | 
|---|
|  | 155 | file. See <a href="security-manager-howto.html">Security Manager HOW-TO</a> | 
|---|
|  | 156 | for further information.</p> | 
|---|
|  | 157 |  | 
|---|
|  | 158 | </blockquote></td></tr></table></td></tr><!--FOOTER SEPARATOR--><tr><td colspan="2"><hr size="1" noshade></td></tr><!--PAGE FOOTER--><tr><td colspan="2"><div align="center"><font size="-1" color="#525D76"><em> | 
|---|
|  | 159 | Copyright © 1999-2008, Apache Software Foundation | 
|---|
|  | 160 | </em></font></div></td></tr></table></body></html> | 
|---|