Spring Boot可执行jar抛出java.lang.ClassNotFoundException:org.apache.tomcat.util.descriptor.web.ServletDef

时间:2016-05-30 16:12:37

标签: java maven spring-boot spring-boot-maven-plugin

我创建了一个简单的测试用例,显示了我目前面临的问题。

我试图做的是手动启动从CommandLineRunner嵌入的Tomcat并手动部署文件系统某处可用的war文件:

package example;

import java.io.File;

import org.apache.catalina.Context;
import org.apache.catalina.LifecycleException;
import org.apache.catalina.startup.Tomcat;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

@Component
public class MyCommandLineRunner implements CommandLineRunner {

    @Override
    public void run(String... args) throws Exception {

        File tomcatBaseDir = new File("<tomcat-base-dir>");
        File warToBeDeployed = new File("<war-to-be-deployed>");

        Tomcat tomcat = new Tomcat();
        tomcat.setPort(8080);
        tomcat.setBaseDir(tomcatBaseDir.getAbsolutePath());
        tomcat.getHost().setAppBase(tomcatBaseDir.getAbsolutePath());
        tomcat.getHost().setAutoDeploy(true);
        tomcat.getHost().setDeployOnStartup(true);

        try {
            tomcat.start();
            System.out.println("Tomcat started on " + tomcat.getHost());
        } catch (LifecycleException e) {
            System.err.println("Tomcat could not be started");
            System.exit(-1);
        }

        Context appContext = tomcat.addWebapp(tomcat.getHost(), "/hello-world", warToBeDeployed.getAbsolutePath());
        System.out.println("Deployed " + appContext.getBaseName() + " on " + tomcat.getHost());

        tomcat.getServer().await();

    }

}

这是我简单的pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.3.5.RELEASE</version>
</parent>

<artifactId>spring-boot-tomcat-embedded-manually</artifactId>
<version>0.0.1-SNAPSHOT</version>

<properties>
    <java.version>1.8</java.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
    </dependency>
    <dependency>
        <groupId>org.apache.tomcat</groupId>
        <artifactId>tomcat-catalina</artifactId>
        <version>${tomcat.version}</version>
    </dependency>
    <dependency>
        <groupId>org.apache.tomcat</groupId>
        <artifactId>tomcat-util</artifactId>
        <version>${tomcat.version}</version>
    </dependency>
    <dependency>
        <groupId>org.apache.tomcat.embed</groupId>
        <artifactId>tomcat-embed-core</artifactId>
    </dependency>
    <dependency>
        <groupId>org.apache.tomcat.embed</groupId>
        <artifactId>tomcat-embed-jasper</artifactId>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

所以这就是:

  1. 如果我从Eclipse中运行应用程序,完全没问题;

  2. 如果我运行“mvn clean package”然后“java -jar [path-to-jar]”,我得到:

    java.lang.ClassNotFoundException: org.apache.tomcat.util.descriptor.web.ServletDef
        at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1305) ~[tomcat-catalina-8.0.33.jar!/:8.0.33]
        at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1139) ~[tomcat-catalina-8.0.33.jar!/:8.0.33]
        at org.apache.tomcat.util.IntrospectionUtils.callMethod1(IntrospectionUtils.java:359) ~[tomcat-util-8.0.33.jar!/:8.0.33]
        at org.apache.tomcat.util.digester.SetNextRule.end(SetNextRule.java:145) ~[tomcat-util-scan-8.0.33.jar!/:8.0.33]
        at org.apache.tomcat.util.digester.Digester.endElement(Digester.java:956) [tomcat-util-scan-8.0.33.jar!/:8.0.33]
        at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.endElement(Unknown Source) [na:1.8.0_91]
        at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanEndElement(Unknown Source) [na:1.8.0_91]
        at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(Unknown Source) [na:1.8.0_91]
        at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(Unknown Source) [na:1.8.0_91]
        at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source) [na:1.8.0_91]
        at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source) [na:1.8.0_91]
        at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source) [na:1.8.0_91]
        at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(Unknown Source) [na:1.8.0_91]
        at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(Unknown Source) [na:1.8.0_91]
        at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(Unknown Source) [na:1.8.0_91]
        at org.apache.tomcat.util.digester.Digester.parse(Digester.java:1451) [tomcat-util-scan-8.0.33.jar!/:8.0.33]
        at org.apache.tomcat.util.descriptor.web.WebXmlParser.parseWebXml(WebXmlParser.java:120) [tomcat-util-scan-8.0.33.jar!/:8.0.33]
        at org.apache.catalina.startup.ContextConfig.webConfig(ContextConfig.java:1115) [tomcat-catalina-8.0.33.jar!/:8.0.33]
        at org.apache.catalina.startup.ContextConfig.configureStart(ContextConfig.java:779) [tomcat-catalina-8.0.33.jar!/:8.0.33]
        at org.apache.catalina.startup.ContextConfig.lifecycleEvent(ContextConfig.java:306) [tomcat-catalina-8.0.33.jar!/:8.0.33]
        at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:95) [tomcat-catalina-8.0.33.jar!/:8.0.33]
        at org.apache.catalina.util.LifecycleBase.fireLifecycleEvent(LifecycleBase.java:90) [tomcat-catalina-8.0.33.jar!/:8.0.33]
        at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5150) [tomcat-catalina-8.0.33.jar!/:8.0.33]
        at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:147) [tomcat-catalina-8.0.33.jar!/:8.0.33]
        at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:725) [tomcat-catalina-8.0.33.jar!/:8.0.33]
        at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:701) [tomcat-catalina-8.0.33.jar!/:8.0.33]
        at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:717) [tomcat-catalina-8.0.33.jar!/:8.0.33]
        at org.apache.catalina.startup.Tomcat.addWebapp(Tomcat.java:558) [tomcat-catalina-8.0.33.jar!/:8.0.33]
        at org.apache.catalina.startup.Tomcat.addWebapp(Tomcat.java:523) [tomcat-catalina-8.0.33.jar!/:8.0.33]
        at example.MyCommandLineRunner.run(MyCommandLineRunner.java:37) [spring-boot-tomcat-embedded-manually-0.0.1-SNAPSHOT.jar!/:0.0.1-SNAPSHOT]
        at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:806) [spring-boot-1.3.5.RELEASE.jar!/:1.3.5.RELEASE]
        at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:790) [spring-boot-1.3.5.RELEASE.jar!/:1.3.5.RELEASE]
        at org.springframework.boot.SpringApplication.afterRefresh(SpringApplication.java:777) [spring-boot-1.3.5.RELEASE.jar!/:1.3.5.RELEASE]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:308) [spring-boot-1.3.5.RELEASE.jar!/:1.3.5.RELEASE]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1191) [spring-boot-1.3.5.RELEASE.jar!/:1.3.5.RELEASE]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1180) [spring-boot-1.3.5.RELEASE.jar!/:1.3.5.RELEASE]
        at example.Main.main(Main.java:10) [spring-boot-tomcat-embedded-manually-0.0.1-SNAPSHOT.jar!/:0.0.1-SNAPSHOT]
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_91]
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_91]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_91]
        at java.lang.reflect.Method.invoke(Unknown Source) ~[na:1.8.0_91]
        at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:54) [spring-boot-tomcat-embedded-manually-0.0.1-SNAPSHOT.jar!/:0.0.1-SNAPSHOT]
        at java.lang.Thread.run(Unknown Source) [na:1.8.0_91]
    
  3. 如果我用maven-shade-plugin替换spring-boot-maven-plugin然后尝试“mvn clean package”,接着是“java -jar [path-to-jar]”一切正常。

  4. 我是否错过了spring-boot-maven-plugin上的一些配置,以使我的可执行jar实际工作?

    更新1

    我的问题与this有些问题有关。我尝试了那里提出的解决方案,确实它在命令行和Eclipse中都有效。

    但是,我仍然不理解在我的具体情况中表现出的不一致行为:

    • 为什么它在eclipse中工作并且它在命令行中不起作用?
    • 如果我用maven-shade-plugin替换spring-boot-maven-plugin,为什么会这样?

2 个答案:

答案 0 :(得分:1)

有点奇怪,我原以为你的pom看起来还不错。也就是说,尝试通过以下方式替换pom中的tomcat条目:

                <dependency>
                        <groupId>org.springframework.boot</groupId>
                        <artifactId>spring-boot-starter-tomcat</artifactId>
                </dependency>

希望有所帮助

答案 1 :(得分:1)

查看this question。您应该将类​​加载器添加到上下文中。直接在行下添加

Context appContext = tomcat.addWebapp(tomcat.getHost(), "/hello-world", warToBeDeployed.getAbsolutePath());

此代码

WebappLoader loader = new WebappLoader(Thread.currentThread().getContextClassLoader());
appContext.setLoader(loader);

在我的情况下有效。