且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用自定义ClassLoader在Tomcat中加载类时出现ClassNotFoundException

更新时间:2023-11-17 13:10:28

还必须定义上下文文件AND作为插件的依赖项(不仅仅是项目依赖项).

You have to also define the context file AND as a dependency of the plugin as well (not just project dependency).

示例pom.xml:

<build>
    <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>tomcat-maven-plugin</artifactId>
        <version>1.1</version>
        <configuration>
            <path>/</path>
            <serverXml>${basedir}/conf/tomcat/server.xml</serverXml>
            <useSeparateTomcatClassLoader>true</useSeparateTomcatClassLoader>
            <contextReloadable>true</contextReloadable>
            <contextFile>${project.basedir}/src/main/webapp/WEB-INF/context.xml</contextFile>
        </configuration>
        <dependencies>
                <dependency>
                    <groupId>com.sample</groupId>
                    <artifactId>customclassloader</artifactId>
                    <version>1.0.0-SNAPSHOT</version>
                </dependency>
        </dependencies>
    </plugin>
</build>
....
....
<dependencies>
    <dependency>
        <groupId>com.sample</groupId>
        <artifactId>customclassloader</artifactId>
        <version>1.0.0-SNAPSHOT</version>
    </dependency>
</dependencies>

示例context.xml:

Example context.xml:

<?xml version="1.0" encoding="UTF-8"?>
    <Context antiJARLocking="true" path="/YourWebApplicationContext">
        <Loader delegate="true" loaderClass="com.sample.customclassloader.MyClassLoader" searchExternalFirst="true"/>
    </Context>

这应该适用于嵌入式tomcat(我使用tomcat7-maven-plugin,版本2.2)

This should work for the embedded tomcat (I use tomcat7-maven-plugin, version 2.2)

如果要将WAR部署到已安装的" tomcat中,请将自定义classloader.jar复制到tomcat lib文件夹中.

If you are deploying the WAR into a "installed" tomcat, copy you custom classloader.jar into the tomcat lib folder.

希望这会有所帮助!