gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 21:35:58 31 4
gpt4 key购买 nike

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

我试图做的是手动启动从 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>

所以这就是发生的事情:
  • 如果我从 Eclipse 中运行应用程序,则完全没有问题;
  • 如果我运行“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]
  • 如果我用 maven-shade-plugin 替换 spring-boot-maven-plugin,然后尝试“mvn clean package”后跟“java -jar [path-to-jar]”,一切正常。

  • 我是否缺少 spring-boot-maven-plugin 上的一些配置以使我的可执行 jar 实际工作?

    更新 1

    我的问题与 this 有点相关一。我尝试了那里提出的解决方案,确实可以从命令行和 Eclipse 工作。

    但是,我仍然不明白在我的具体案例中表现出的不一致行为:
  • 为什么它可以在 eclipse 中运行,而不能在命令行中运行?
  • 为什么如果我用 maven-shade-plugin 替换 spring-boot-maven-plugin 会起作用?
  • 最佳答案

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

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

    这段代码
    WebappLoader loader = new WebappLoader(Thread.currentThread().getContextClassLoader());
    appContext.setLoader(loader);

    它在我的情况下有效。

    关于java - Spring Boot 可执行 jar 抛出 java.lang.ClassNotFoundException : org. apache.tomcat.util.descriptor.web.ServletDef,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37529952/

    31 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com