Tomcat 9.0.12版启动问题

时间:2018-09-28 08:24:55

标签: java tomcat

我正在尝试在运行jre 1.8的tomcat 9.0.12版和Eclipse Oxygen上部署应用程序。

但是我经常遇到以下例外情况,

Caused by: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[]]
    at org.apache.catalina.util.LifecycleBase.handleSubClassException(LifecycleBase.java:441)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:198)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1429)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1419)
    at java.util.concurrent.FutureTask.run(Unknown Source)
    at org.apache.tomcat.util.threads.InlineExecutorService.execute(InlineExecutorService.java:75)
    at java.util.concurrent.AbstractExecutorService.submit(Unknown Source)
    at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:944)
    ... 21 more
Caused by: java.lang.NoSuchMethodError: javax.servlet.ServletContext.getClassLoader()Ljava/lang/ClassLoader;

我的类路径中有一个servlet-api-2.5.jar方法,其中有一个getClassLoader。不知道我做错了什么。

有人可以帮我解决一个问题吗?

1 个答案:

答案 0 :(得分:1)

尽管您声称servlet-api-2.5.jar中的ServletContext不能包含有问题的方法,但实际上它是否包含文件名建议的Servlet 2.5规范。

ServletContext.getClassLoader在Servlet规范的3.0版中引入:

/**
 * Get the web application class loader associated with this ServletContext.
 *
 * @return The associated web application class loader
 *
 * @throws UnsupportedOperationException    If called from a
 *    {@link ServletContextListener#contextInitialized(ServletContextEvent)}
 *    method of a {@link ServletContextListener} that was not defined in a
 *    web.xml file, a web-fragment.xml file nor annotated with
 *    {@link javax.servlet.annotation.WebListener}. For example, a
 *    {@link ServletContextListener} defined in a TLD would not be able to
 *    use this method.
 * @throws SecurityException if access to the class loader is prevented by a
 *         SecurityManager
 * @since Servlet 3.0
 */
public ClassLoader getClassLoader();

Apache Tomcat 9 implements Servlet 4.0一样,您不应使用不同的servlet api版本污染您的类路径。

Tomcat已随附servlet-api.jar目录中提供的正确apache-tomcat/lib。您不应该将该jar的任何版本添加到Web应用程序。如果您使用的是maven,则可以在provided范围内添加对servlet 4 api的引用,因此您的IDE和构建工具knwo将在运行时可用,但不得随工件一起提供:

<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <version>4.0.1</version>
    <scope>provided</scope>
</dependency>

或者完全匹配所选的Apache Tomcat版本:

<dependency>
    <groupId>org.apache.tomcat</groupId>
    <artifactId>tomcat-servlet-api</artifactId>
    <version>9.0.12</version>
    <scope>provided</scope>
</dependency>