Home  >  Q&A  >  body text

java - 关于maven tomcat启动错误

maven(命令为 clean tomcat7:run)启动tomcat提示错误:
org.springframework.web.SpringServletContainerInitializer cannot be cast to javax.servlet.ServletContainerInitializer

自己认为是版本冲突问题,但是不知道怎么解决。
查了下,有人说是pom.xml中servlet jar包部分的<scope/>应该设为provided,但自己确实是这么配置的。
下面是pom.xml部分内容:

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

各位有谁遇到过或者对此有个人见解,麻烦给予点拨,谢谢!


----------------------Update-------------------------
排错能力实在有限,还是没能解决问题。
贴一下比较完整的信息吧:

Caused by: java.lang.ClassCastException: org.springframework.web.SpringServletContainerInitializer cannot be cast to javax.servlet.ServletContainerInitializer
    at org.apache.catalina.startup.ContextConfig.getServletContainerInitializer(ContextConfig.java:1670)
    at org.apache.catalina.startup.ContextConfig.getServletContainerInitializers(ContextConfig.java:1652)
    at org.apache.catalina.startup.ContextConfig.processServletContainerInitializers(ContextConfig.java:1562)
    at org.apache.catalina.startup.ContextConfig.webConfig(ContextConfig.java:1270)
    at org.apache.catalina.startup.ContextConfig.configureStart(ContextConfig.java:878)
    at org.apache.catalina.startup.ContextConfig.lifecycleEvent(ContextConfig.java:376)
    at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:119)
    at org.apache.catalina.util.LifecycleBase.fireLifecycleEvent(LifecycleBase.java:90)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5322)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    ... 6 more

根据以上信息我猜测是tomcat本身的api和springframework.web包有冲突(spring版本4.2.4.RELEASE)
spring-web含有一个javax.servlet-api的依赖:

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

自己pom.xml配置的servlet是

<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>servlet-api</artifactId>
    <version>${servlet-api.version}</version> <!--引用2.5-->
    <scope>provided</scope>
</dependency>            
<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>jsp-api</artifactId>
    <version>${jsp-api.version}</version>  <!--引用2.0-->
    <scope>provided</scope>
</dependency>

tomcat使用的是:

<groupId>org.apache.tomcat.maven</groupId>
    <artifactId>tomcat7-maven-plugin</artifactId>
    <configuration>
    <path>/</path>
    <port>8080</port>
</configuration>

本来想换tomcat8试试的,但是好像也没有tomcat8-maven-plugin
试过不包含其它jar包也还是如此,所以该如何是好呢?(╥╯^╰╥)

PHPzPHPz2763 days ago805

reply all(5)I'll reply

  • 伊谢尔伦

    伊谢尔伦2017-04-18 10:52:15

    Specify the version of Servlet API and try it:

    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>3.1.0</version>
        <scope>provided</scope>
    </dependency>
    
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jsp-api</artifactId>
        <version>2.0</version>
        <scope>provided</scope>
    </dependency>

    reply
    0
  • 伊谢尔伦

    伊谢尔伦2017-04-18 10:52:15

    There is a package conflict, check if there is servlet-api in other referenced jar packages, and use the exclusion tag to remove servlet-api

    reply
    0
  • 巴扎黑

    巴扎黑2017-04-18 10:52:15

    It should be that the servlet api package that spring depends on conflicts with the servlet jar package that comes with tomcat 7.
    Exclude all poms that depend on the servlet api. You can use the exclusion tag.

    reply
    0
  • ringa_lee

    ringa_lee2017-04-18 10:52:15

    1. There may be conflicts with other packages
    2. I guess you are not using springboot. If it is springboot, it itself contains a complete tomcat, so an error will be reported. I remember it seems that it will report an error. .

    reply
    0
  • 怪我咯

    怪我咯2017-04-18 10:52:15


    Check if there is a package conflict. IntelliJ has a button here (the problem seems to be a red line)
    Eclipse on Baidu, it should be almost the same. If there is a conflict, try exclusion
    Here it is A similar one to yours is also <scope>provided</scope>, but exclusion is still required
    http://powertech.iteye.com/bl...

    Or try changing the servlet-api to 3.1.0. The link describes that there is a problem with version 3.0.1 on stackoverflow

    reply
    0
  • Cancelreply