TOMCAT class loader - tomcat本身的文档就很详细了

本文详细介绍了Apache Tomcat 5.5 Servlet/JSP容器的类加载机制,包括不同类加载器的作用、它们之间的继承关系及如何正确放置类和资源文件等内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

The Jakarta Project       The Apache Tomcat Servlet/JSP Container

Links

User Guide

Reference

Apache Tomcat Development

The Apache Tomcat 5.5 Servlet/JSP Container

Class Loader HOW-TO

Printer Friendly Version
print-friendly
version
Quick Start

The following rules cover about 95% of the decisions that application developers and deployers must make about where to place class and resource files to make them available to web applications:

  • For classes and resources specific to a particular web application, place unpacked classes and resources under /WEB-INF/classes of your web application archive, or place JAR files containing those classes and resources under /WEB-INF/lib of your web application archive.
  • For classes and resources that must be shared across all web applications, place unpacked classes and resources under $CATALINA_BASE/shared/classes, or place JAR files containing those classes and resources under $CATALINA_BASE/shared/lib.
Overview

Like many server applications, Tomcat 5 installs a variety of class loaders (that is, classes that implement java.lang.ClassLoader) to allow different portions of the container, and the web applications running on the container, to have access to different repositories of available classes and resources. This mechanism is used to provide the functionality defined in the Servlet Specification, version 2.4 -- in particular, Sections 9.4 and 9.6.

In a J2SE 2 (that is, J2SE 1.2 or later) environment, class loaders are arranged in a parent-child tree. Normally, when a class loader is asked to load a particular class or resource, it delegates the request to a parent class loader first, and then looks in its own repositories only if the parent class loader(s) cannot find the requested class or resource. The model for web application class loaders differs slightly from this, as discussed below, but the main principles are the same.

When Tomcat 5 is started, it creates a set of class loaders that are organized into the following parent-child relationships, where the parent class loader is above the child class loader:

      Bootstrap
|
System
|
Common
/ /
Catalina Shared
/ /
Webapp1 Webapp2 ...

The characteristics of each of these class loaders, including the source of classes and resources that they make visible, are discussed in detail in the following section.

Class Loader Definitions

As indicated in the diagram above, Tomcat 5 creates the following class loaders as it is initialized:

  • Bootstrap - This class loader contains the basic runtime classes provided by the Java Virtual Machine, plus any classes from JAR files present in the System Extensions directory ($JAVA_HOME/jre/lib/ext). NOTE - Some JVMs may implement this as more than one class loader, or it may not be visible (as a class loader) at all.
  • System - This class loader is normally initialized from the contents of the CLASSPATH environment variable. All such classes are visible to both Tomcat internal classes, and to web applications. However, the standard Tomcat 5 startup scripts ($CATALINA_HOME/bin/catalina.sh or %CATALINA_HOME%/bin/catalina.bat) totally ignore the contents of the CLASSPATH environment variable itself, and instead build the System class loader from the following repositories:
    • $CATALINA_HOME/bin/bootstrap.jar - Contains the main() method that is used to initialize the Tomcat 5 server, and the class loader implementation classes it depends on.
    • $JAVA_HOME/lib/tools.jar - Contains the "javac" compiler used to convert JSP pages into servlet classes.
    • $CATALINA_HOME/bin/commons-logging-api.jar - Jakarta commons logging API.
    • $CATALINA_HOME/bin/commons-daemon.jar - Jakarta commons daemon API.
    • jmx.jar - The JMX 1.2 implementation.
  • Common - This class loader contains additional classes that are made visible to both Tomcat internal classes and to all web applications. Normally, application classes should NOT be placed here. All unpacked classes and resources in $CATALINA_HOME/common/classes, as well as classes and resources in JAR files under the $CATALINA_HOME/commons/endorsed, $CATALINA_HOME/commons/i18n and $CATALINA_HOME/common/lib directories, are made visible through this class loader. By default, that includes the following:
    • commons-el.jar - Jakarta commons el, implementing the expression language used by Jasper.
    • jasper-compiler.jar - The JSP 2.0 compiler.
    • jasper-compiler-jdt.jar - The Eclipse JDT Java compiler.
    • jasper-runtime.jar - The JSP 2.0 runtime.
    • jsp-api.jar - The JSP 2.0 API.
    • naming-common.jar - The JNDI implementation used by Tomcat 5 to represent in-memory naming contexts.
    • naming-factory.jar - The JNDI implementation used by Tomcat 5 to resolve references to enterprise resources (EJB, connection pools).
    • naming-factory-dbcp.jar - Jakarta commons DBCP, providing a JDBC connection pool to web applications. The classes have been moved out of their default org.apache.commons package.
    • naming-java.jar - Handler for the java: namespace.
    • naming-resources.jar - The specialized JNDI naming context implementation used to represent the static resources of a web application. This is not related to the support of the J2EE ENC, and cannot be removed.
    • servlet-api.jar - The Servlet 2.4 API.
    • tomcat-i18n-**.jar - Optional JARs containing resource bundles for other languages. As default bundles are also included in each individual JAR, they can be safely removed if no internationalization of messages is needed.
  • Catalina - This class loader is initialized to include all classes and resources required to implement Tomcat 5 itself. These classes and resources are TOTALLY invisible to web applications. All unpacked classes and resources in $CATALINA_HOME/server/classes, as well as classes and resources in JAR files under $CATALINA_HOME/server/lib, are made visible through this class loader. By default, that includes the following:
    • catalina.jar - Implementation of the Catalina servlet container portion of Tomcat 5.
    • catalina-ant.jar - Some Ant tasks which can be used to manage Tomcat using the manager web application.
    • catalina-optional.jar - Some optional components of Catalina.
    • commons-modeler.jar - A model MBeans implementation used by Tomcat to expose its internal objects through JMX.
    • servlets-xxxxx.jar - The classes associated with each internal servlet that provides part of Tomcat's functionality. These are separated so that they can be completely removed if the corresponding service is not required, or they can be subject to specialized security manager permissions.
    • tomcat-coyote.jar - Coyote API.
    • tomcat-http.jar - Standalone Java HTTP/1.1 connector.
    • tomcat-ajp.jar - Classes for the Java portion of the AJP web server connector, which allows Tomcat to run behind web servers such as Apache and iPlanet iAS and iWS.
    • tomcat-util.jar - Utility classes required by some Tomcat connectors.
  • Shared - This class loader is the place to put classes and resources that you wish to share across ALL web applications (unless Tomcat internal classes also need access, in which case you should put them in the Common class loader instead). All unpacked classes and resources in $CATALINA_BASE/shared/classes, as well as classes and resources in JAR files under $CATALINA_BASE/shared/lib, are made visible through this class loader. If multiple Tomcat instances are run from the same binary using the $CATALINA_BASE environment variable, then this classloader repositories are relative to $CATALINA_BASE rather than $CATALINA_HOME.
  • WebappX - A class loader is created for each web application that is deployed in a single Tomcat 5 instance. All unpacked classes and resources in the /WEB-INF/classes directory of your web application archive, plus classes and resources in JAR files under the /WEB-INF/lib directory of your web application archive, are made visible to the containing web application, but to no others.

As mentioned above, the web application class loader diverges from the default Java 2 delegation model (in accordance with the recommendations in the Servlet Specification, version 2.3, section 9.7.2 Web Application Classloader). When a request to load a class from the web application's WebappX class loader is processed, this class loader will look in the local repositories first, instead of delegating before looking. There are exceptions. Classes which are part of the JRE base classes cannot be overriden. For some classes (such as the XML parser components in J2SE 1.4+), the J2SE 1.4 endorsed feature can be used (see the common classloader definition above). In addition, for the following class patterns, the classloader will always delegate first (and load the class itself if no parent classloader loads it):

  • javax.*
  • org.xml.sax.*
  • org.w3c.dom.*
  • org.apache.xerces.*
  • org.apache.xalan.*
Last, any JAR containing servlet API classes will be ignored by the classloader. All other class loaders in Tomcat 5 follow the usual delegation pattern.

Therefore, from the perspective of a web application, class or resource loading looks in the following repositories, in this order:

  • Bootstrap classes of your JVM
  • System class loader classses (described above)
  • /WEB-INF/classes of your web application
  • /WEB-INF/lib/*.jar of your web application
  • $CATALINA_HOME/common/classes
  • $CATALINA_HOME/common/endorsed/*.jar
  • $CATALINA_HOME/common/i18n/*.jar
  • $CATALINA_HOME/common/lib/*.jar
  • $CATALINA_BASE/shared/classes
  • $CATALINA_BASE/shared/lib/*.jar
XML Parsers and J2SE 1.4

Among many other changes, the J2SE 1.4 release packages the JAXP APIs, and a version of Xerces, inside the JRE. This has impacts on applications that wish to use their own XML parser.

In previous versions of Tomcat 5, you could simply replace the XML parser in the $CATALINA_HOME/common/lib directory to change the parser used by all web applications. However, this technique will not be effective when you are running on J2SE 1.4, because the usual class loader delegation process will always choose the implementation inside the JDK in preference to this one.

JDK 1.4 supports a mechanism called the "Endorsed Standards Override Mechanism" to allow replacement of APIs created outside of the JCP (i.e. DOM and SAX from W3C). It can also be used to update the XML parser implementation. For more information, see: http://java.sun.com/j2se/1.4/docs/guide/standards/index.html.

Tomcat utilizes this mechanism by including the system property setting -Djava.endorsed.dirs=$CATALINA_HOME/common/endorsed in the command line that starts the container. Therefore, you can replace the parser that is installed in this directory, and it will get used even on a JDK 1.4 system.

Running under a security manager

When running under a security manager the locations from which classes are permitted to be loaded will also depend on the contents of your policy file. See Security Manager HOW-TO for further information.


Copyright © 1999-2005, Apache Software Foundation
<think>首先,用户的问题是关于TomcatLoader Debug设置为1的配置。用户提到“Tomcat Loader Debug=1 configuration”,这似乎是关于Tomcat的类加载器调试设置。 从系统级指令中,我需要: - 所有行内数学表达式用$...$格式。 - 独立公式用$$...$$格式并单独成段。 - LaTeX语法正确。 - 使用中文回答。 - 生成相关问题。 - 引用的段落末尾自然地添加引用标识,如[^1]。 - 回答结构清晰,帮助用户逐步解决问题。 - 保证回答真实可靠。 参考站内引用: - 引用[1]: 关于Tomcat类加载顺序的描述。 - 引用[2]: 关于Tomcat日志级别的设置。 用户意图似乎是查找有关TomcatLoader的Debug配置设置为1的信息,可能是为了调试或排查问题。 我需要理解什么是“Loader Debug=1”。在Tomcat中,这通常指的是启用类加载器的调试日志级别。Tomcat的日志系统基于Java Logging API,可以通过配置logging.properties文件来设置日志级别。 在引用[2]中,提到了如何设置日志级别为DEBUG,这包括handlers的配置。类似地,对于类加载器调试,可能需要设置特定的日志级别。 Tomcat的类加载器调试可以通过设置日志级别来实现。通常,相关的日志记录器是org.apache.catalina.loader,我们可以将其级别设置为FINE或FINER或ALL来启用调试输出。用户提到“Debug=1”,这可能是某种简写或特定配置,但标准方式是使用日志级别。 在Tomcat中,设置调试级别: - 编辑conf/logging.properties文件。 - 找到或添加相关日志记录器的条目。 - 例如,设置org.apache.catalina.loader.level = FINE 来启用类加载器调试信息。 用户说“Debug=1”,这可能指的是在配置中设置级别为1,但Java Logging API使用级别如SEVERE、WARNING、INFO、CONFIG、FINE、FINER、FINEST。数字级别对应: - SEVERE: 1000 - WARNING: 900 - INFO: 800 - CONFIG: 700 - FINE: 500 - FINER: 400 - FINEST: 300 所以“Debug=1”可能是个误解;标准方式是设置级别为FINE或更低。 在Tomcat的上下文中,有时可以通过JVM参数或特定属性启用调试,但最常见的是通过logging.properties。 回顾用户的问题:"Tomcat Loader Debug=1 configuration",并参考意图是“查找有关TomcatLoader的Debug配置设置为1的信息”。 在引用[1]中,提到Tomcat类加载顺序,但没有直接说Debug设置。引用[2]是关于设置日志级别为DEBUG。 所以,我应该解释如何配置Tomcat以启用类加载器的调试日志。 步骤: 1. 找到Tomcat的conf目录下的logging.properties文件。 2. 编辑该文件,添加或修改日志记录器的级别。 3. 例如,设置org.apache.catalina.loader.level = FINE 4. 重启Tomcat以使更改生效。 用户提到“Debug=1”,这可能源于某些文档或特定上下文。在一些系统中,“1”可能表示启用调试,但在标准Tomcat中,它是通过日志级别。 在Tomcat文档或常见实践中,设置类加载器调试通常是通过日志级别。 另外,Tomcat有一个特定的属性:在context.xml中,有<Loader>元素,但它的debug属性在较新版本中可能已弃用或不常用。例如,在旧Tomcat中,<Loader className="org.apache.catalina.loader.WebappLoader" debug="1"/>,但现代Tomcat版本使用标准日志。 在Tomcat 8及以后,推荐使用Java Logging或Log4j等日志框架,而不是特定于容器的debug属性。 所以,在回答中,我应该基于标准日志配置。 结构回答: - 介绍问题:解释Tomcat类加载器调试。 - 逐步指导如何配置。 - 提供示例配置。 - 解释输出。 - 添加引用标识。 - 最后生成相关问题。 使用中文。 生成相关问题:基于系统指令,我需要生成相关问题,如示例中的“相关问题”。 相关问题应该与主题相关,例如: 1. 如何解读Tomcat类加载器调试日志? 2. Tomcat类加载器常见问题有哪些? 3. 如何优化Tomcat类加载性能? 确保回答真实可靠:基于Tomcat官方文档或常见知识。 现在,构建回答。</think>### Tomcat Loader Debug=1 配置指南 用户的问题是关于在Tomcat中配置类加载器(Loader)调试模式为“Debug=1”的信息。这通常用于调试类加载问题,如类冲突、加载失败或顺序错误(如引用[1]中描述的类加载顺序问题)。在Tomcat中,“Debug=1”可能是一个简化的表达,实际指的是通过日志系统启用类加载器的调试输出。标准做法是配置日志级别(如FINE或FINER),而非直接设置“Debug=1”属性(现代Tomcat版本中,特定debug属性已弃用)。下面我将逐步解释如何配置,确保回答基于Tomcat官方文档和常见实践[^1][^2]。 #### 步骤1: 理解Tomcat类加载器调试 - Tomcat的类加载器(如`WebappClassLoader`)负责加载Web应用中的类。调试模式会输出详细日志,包括类加载路径、资源查找和错误信息。 - “Debug=1”可能源于旧版Tomcat(如Tomcat 5/6),其中在`context.xml`中设置`<Loader debug="1">`,但Tomcat 8及更高版本推荐使用Java Logging API或Log4j进行调试[^1]。 - 调试级别对应Java Logging API的数值(如FINE=500),数字“1”可能是个误用;标准级别包括: - SEVERE (1000), WARNING (900), INFO (800), CONFIG (700), FINE (500), FINER (400), FINEST (300) - 类加载器调试通常设置为FINE或更低(数值更小),以捕获详细信息。 #### 步骤2: 配置日志级别启用调试 现代TomcatTomcat 8+)使用`conf/logging.properties`文件控制日志。以下是配置步骤: 1. **编辑日志配置文件**: - 打开Tomcat安装目录下的`conf/logging.properties`文件。 - 添加或修改与类加载器相关的日志记录器条目。关键日志记录器是`org.apache.catalina.loader`(针对类加载器)和`org.apache`(更广泛的调试)。 - 示例配置(设置级别为FINE,相当于调试输出): ```properties # 设置类加载器日志级别为FINE(调试级别) org.apache.catalina.loader.level = FINE # 可选:设置更广泛的Tomcat日志级别以捕获更多上下文 org.apache.level = FINE # 确保处理器(Handler)输出调试信息,如引用[2]中的handlers配置 handlers = 1catalina.org.apache.juli.FileHandler, java.util.logging.ConsoleHandler 1catalina.org.apache.juli.FileHandler.level = FINE java.util.logging.ConsoleHandler.level = FINE ``` - 如果文件中已有类似条目,只需修改`level`值。例如,将默认的`INFO`改为`FINE`。 2. **重启Tomcat**: - 保存文件后,重启Tomcat服务器以使配置生效。在Linux中,使用命令: ```bash ./catalina.sh stop ./catalina.sh start ``` - 在Windows中,使用`catalina.bat`脚本或服务管理器。 3. **验证调试输出**: - 调试日志会输出到Tomcat的日志文件(如`logs/catalina.out`或`logs/localhost.log`)。 - 示例日志条目(显示类加载细节): ``` [FINE] WebappClassLoader: Loading class 'com.example.MyClass' from /WEB-INF/classes [FINE] WebappClassLoader: Resource not found: com/example/MyClass.class ``` - 如果输出不足,可降低级别到`FINER`(数值400)以获取更多细节。 #### 步骤3: 替代配置方法 - **JVM启动参数**:如果无法修改`logging.properties`,可通过JVM参数动态设置调试级别: ```bash ./catalina.sh run -Djava.util.logging.config.file=conf/logging.properties -Dorg.apache.catalina.loader.level=FINE ``` - **旧版Tomcat的debug属性**:如果使用Tomcat 7或更早版本,在`context.xml`中添加`<Loader>`元素(但不推荐,因可能引发兼容性问题): ```xml <Context> <Loader className="org.apache.catalina.loader.WebappLoader" debug="1"/> </Context> ``` 注意:`debug="1"`启用基本调试,但现代版本已集成到日志系统[^1]。 #### 常见问题与注意事项 - **调试输出过多**:设置`FINE`级别可能生成大量日志。建议在测试环境使用,生产环境恢复为`INFO`。 - **类加载顺序问题**:如引用[1]所述,Tomcat类加载顺序(JVM > WEB-INF > CommonClassLoader)可能导致冲突。调试日志可帮助识别此类问题,例如类被多次加载。 - **性能影响**:启用调试会轻微增加开销,但通常可接受用于临时诊断。 - **日志文件管理**:确保日志文件不会过大,可配置`FileHandler`的轮转策略(如`1catalina.org.apache.juli.FileHandler.maxFiles`属性)。 通过以上配置,您可以捕获类加载的详细过程,便于排查类缺失、版本冲突或自定义加载器问题[^1][^2]。如果问题未解决,请提供更多上下文(如Tomcat版本和错误日志)。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值