Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maven + Spring + Dynamic Web Module ( Eclipse ) = java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener

My problem is that even set the "Deployment Assembly" to include the maven dependencies, this giving that my class is not being found, I do not know what else to do.

And I'm just noticing that with the ContextLoaderListener class since other classes seem to be included in my package.

My file 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/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.mkyong.common</groupId>
    <artifactId>SpringMVC</artifactId>
    <packaging>war</packaging>
    <version>1.0-SNAPSHOT</version>
    <name>SpringMVC Maven Webapp</name>
    <url>http://maven.apache.org</url>
    <dependencies>
        <!-- <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> 
            <version>3.8.1</version> <scope>test</scope> </dependency> -->

        <!-- Spring framework -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring</artifactId>
            <version>2.5.6</version>
        </dependency>

        <!-- Spring MVC framework -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>2.5.6</version>
        </dependency>

        <!-- JSTL -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.1.2</version>
        </dependency>

        <dependency>
            <groupId>taglibs</groupId>
            <artifactId>standard</artifactId>
            <version>1.1.2</version>
        </dependency>

        <!-- for compile only, your container should have this -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5</version>
            <scope>provided</scope>
        </dependency>

    </dependencies>

    <build>
        <finalName>SpringMVC</finalName>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>tomcat-maven-plugin</artifactId>
                <version>1.0-beta-1</version>
                <configuration></configuration>
            </plugin>
        </plugins>
    </build>

</project>

My file web.xml

<web-app id="WebApp_ID" version="2.4"
    xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
    http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

    <display-name>Spring Web MVC Application</display-name>

    <servlet>
        <servlet-name>mvc-dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>mvc-dispatcher</servlet-name>
        <url-pattern>*.htm</url-pattern>
    </servlet-mapping>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/mvc-dispatcher-servlet.xml</param-value>
    </context-param>

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

</web-app>
like image 996
Victor Viana Avatar asked Apr 06 '12 17:04

Victor Viana


2 Answers

This problem seems to be either due to:

  1. Some library version mismatch (ie. java version to compile != java version on web app server).
  2. Required Spring library not included during deployment.
  3. Maven archetypes used to setup the project

Perform these steps to try fix this issue:

  • In pom.xml > if you use maven-compiler-plugin, double check this is matching the version of the library used, if not change it (ie. 1.7 NOT 1.6).

    <plugin>
     <artifactId>maven-compiler-plugin</artifactId>
     <configuration>
      <source>1.7</source>
      <target>1.7</target>
     </configuration>
    </plugin>
    
  • If you changed the library version in the above step, you then must do: Right Click on Project > Maven > Update Project... If an error is thrown during this step, you may find yourself stuck for good. I had this issue and could not resolve it. I ended up giving up creating my "Spring MVC with Maven" in Eclipse using archetypes. I believe using archetypes messed things up..in my case.

  • Right Click on Project > Properties > Java Build Path > JRE System Library > Edit > choose correct library version

  • Right Click on Project > Properties > Project Facets > Dynamic Web module (should be checked) > Runtimes tab on the right side of the panel > choose the server created (you must have added a server previously).

  • Right Click on Project > Properties > Deployment Assembly > Add > Java Build Path Entries > select the Spring dependencies. This is from ClassNotFoundException while loading ContextLoaderListener Note: You must perform this step every time after doing a "Maven > Update Project" in eclipse

  • Remember to clean your project before to deploy it: Project > Clean ... > select project > clean

  • Deploy your project and see it NOT break... hopefully

In my case, I had to restart a new project without using maven archetypes to get going. I used these guidelines http://gkokkinos.wordpress.com/2012/01/02/setting-up-a-spring-mvc-project-with-maven-in-eclipse/ . I still had an error thrown, but adding the Spring dependencies via Deployment Assembly (as described above) fixed things up.

like image 63
Adrien Be Avatar answered Oct 12 '22 05:10

Adrien Be


I had this same problem. Simple solution is to right click the top level project -> Properties -> Deployment Assembly and include the 'Maven Dependencies'. In my case this is where my problem was. The class DispatcherServlet was not being included in the war file, therefore it was not found by Tomcat when I deployed it to that server.

like image 21
Cam Avatar answered Oct 12 '22 04:10

Cam