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

My problem is that even setting up a "deployment build" to include maven dependencies means that my class is not found, I don't know what else to do.

And I just notice this with the ContextLoaderListener class, since other classes seem to be included in my package.

My pom.xml file

 <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 web.xml file

 <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> 
+5
source share
3 answers

This problem occurs either due to:

  • Some mismatch of the library version (for example, the java version for compilation! = Java version on the web application server).
  • Required Spring library not included during deployment.
  • Maven archetypes used to set up a project

Follow these steps to fix this problem:

  • In pom.xml> if you are using the maven-compiler-plugin, double check if this matches the library you are using, if you don't change it (i.e. 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 should do the following: Right-click on the project> Maven> Update Project ... If an error occurs during this step, you may be in good shape, I had this problem and I could not solve it. I ended up giving up creating my "Spring MVC with Maven" in Eclipse using archetypes. I believe the use of archetypes is confused. In my case.

  • Right click on Project> Properties> Java Build Path> JRE System Library> Edit> select the correct library version

  • Right-click Project> Properties> Fac Facets> Dynamic Web Module (must be checked)> Runtimes tab in the right part of the panel> select the server you created (you should have added the server before).

  • Right-click Project> Properties> Assembly> Add> Java Build Path Entries> select Spring dependencies. This is from a ClassNotFoundException when loading ContextLoaderListener Note. You must complete this step every time after completing the "Maven Update Project> in eclipse

  • Remember to clean the project before deploying it: Project> Clean ...> select the project> clean

  • Expand your project and see that it does not break ... I hope

In my case, I had to restart the new project without using maven archetypes to get started. I used these recommendations http://gkokkinos.wordpress.com/2012/01/02/setting-up-a-spring-mvc-project-with-maven-in-eclipse/ . I still had a bug, but adding Spring dependencies through the deployment build (as described above) fixed.

+10
source

I had the same problem. A simple solution is to right-click the top-level project -> Properties -> Deploy Build and enable "Maven Dependencies". In my case, it was my problem. The DispatcherServlet class was not included in the war file, so it was not found by Tomcat when I deployed it to this server.

+2
source

I had a similar problem and I followed the steps below to fix it.

  • Click the Server tab in eclipse. Right click on your server.
  • Clear (Tomcat) Working Directory
  • Clear (this is different from cleaning the working directory, which you can see in the context menu)
0
source

All Articles