java.lang.NoClassDefFoundError:javax / servlet / ServletContextListener

时间:2013-08-14 18:29:43

标签: eclipse maven tomcat7

我试图将spring与JSF集成,我在web.xml中添加了以下内容,

<listener>  
<listener-class>
   org.springframework.web.context.ContextLoaderListener
</listener-class>  
</listener>  

<listener> 
<listener-class>
 org.springframework.web.context.request.RequestContextListener
</listener-class>    
</listener> 

我还在eclipse

中的tomcat类路径中添加了spring-web和common logging

没有构建错误,但在tomcat 7上运行时遇到以下错误,

  java.lang.NoClassDefFoundError: javax/servlet/ServletContextListener
at java.lang.ClassLoader.findBootstrapClass(Native Method)
at java.lang.ClassLoader.findBootstrapClassOrNull(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)

   java.lang.NoClassDefFoundError: javax/servlet/ServletRequestListener
at java.lang.ClassLoader.findBootstrapClass(Native Method)
at java.lang.ClassLoader.findBootstrapClassOrNull(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)

这些是我的设置,

  • tomcat lib中有一个servlet-api jar

  • 在我的Pom.xml中,我已经给出了

    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>servlet-api</artifactId>
        <version>2.5</version>
        <scope>compile</scope>
    </dependency> 
    
     and            
        <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-api</artifactId>
            <version>6.0</version>
        </dependency> 
    

我已经尝试在pom.xml中注释了这个条目,但也没有运气。 任何帮助将不胜感激。

这是我的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>
<groupId>TimeTracker</groupId>
<artifactId>TimeTracker</artifactId>
<version>0.0.1-SNAPSHOT</version>

<repositories>
    <repository>
        <id>prime-repo</id>
        <name>PrimeFaces Maven Repository</name>
        <url>http://repository.primefaces.org</url>
    </repository>

    <repository>
<id>Java.Net</id>
<url>http://download.java.net/maven/2/</url>
 </repository>
</repositories>

<dependencies>
    <dependency>
        <groupId>commons-io</groupId>
        <artifactId>commons-io</artifactId>
        <version>1.3.2</version>
    </dependency>
    <dependency>
        <groupId>commons-fileupload</groupId>
        <artifactId>commons-fileupload</artifactId>
        <version>1.2.2</version>
    </dependency>

    <!-- J2ee -->

          <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-api</artifactId>
            <version>6.0</version>
             <scope>provided</scope> 
        </dependency> 

    <!-- Spring framework -->

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>3.2.3.RELEASE</version>
        <scope>provided</scope>
    </dependency> 

<!-- JSR-330 -->
     <dependency>
        <groupId>javax.inject</groupId>
        <artifactId>javax.inject</artifactId>
        <version>1</version>
    </dependency> 

    <!-- PrimeFaces -->
    <dependency>
        <groupId>org.primefaces</groupId>
        <artifactId>primefaces</artifactId>
        <version>3.5</version>
    </dependency>


    <!-- JSF -->
    <dependency>
        <groupId>com.sun.faces</groupId>
        <artifactId>jsf-api</artifactId>
        <version>2.1.13</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>com.sun.faces</groupId>
        <artifactId>jsf-impl</artifactId>
        <version>2.1.13</version>
        <scope>provided</scope>
    </dependency>

     <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
        <version>1.2</version>
        <scope>provided</scope>
    </dependency> 

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

     <dependency>
        <groupId>javax.servlet.jsp</groupId>
        <artifactId>jsp-api</artifactId>
        <version>2.1</version>
    </dependency>

    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>servlet-api</artifactId>
        <version>2.5</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
        <version>1.2</version>
        <scope>provided</scope>
    </dependency>
</dependencies>
<build>
    <sourceDirectory>src</sourceDirectory>
    <plugins>
        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.1</version>
            <configuration>
                <source>1.6</source>
                <target>1.6</target>
            </configuration>
        </plugin>
    </plugins>
</build>

我注意到的另一件事是在部署期间复制的jar文件。我在日食中运行tomcat 7中的项目。这是部署程序集中的条目 / src WEB-INF / classes /网页内容 / JSF 2.1(Apache MyFaces 2.1.5)WEB-INF / lib

没关系?或者我是否还必须添加maven依赖项?但是在添加maven依赖项时,项目和maven上会出现错误标记 - &gt;项目更新。更新时,交叉错误和maven依赖项也会消失。好吧,我正在尝试将JSF与spring集成

谢谢, 飞马

2 个答案:

答案 0 :(得分:2)

作为一种标准做法,你不应该将任何与servlet相关的jar与war文件捆绑在一起,让你的app服务器(在你的情况下是tomcat)在运行时提供所需的类。

将范围更改为已提供,再次捆绑war文件并尝试部署。

    <scope>provided</scope>

答案 1 :(得分:1)

servlet-api是一个应该具有provided范围的依赖项的典型示例,请参阅Introduction to dependency mechanism