Tomcat & Spring Web - Class Not Found Exception org.springframework.web.context.ContextLoaderListener

67,268

Solution 1

I faced the same problem few days ago with m2eclipse (without WTP integration add on available via m2eclipse-extras) on Eclipse Indigo. I created a Maven Web Module and manually added the Dynamic Web Module facet to it. When I deployed it on Eclipse WST Tomcat Server, it appeared that my Maven dependencies are not getting pushed to the server when the project is published to it.

The solution was simple. Right Click on your web project in Project Explorer -> select 'Properties'. Under project properties, select 'Deployment Assembly'.

The Deployment Assembly property page shows the content that will be published as a assembled artifact by Eclipse to the server. You need to tell Eclipse that you want all your Maven dependencies to be published too.

To do that, click 'Add' button, then select 'Java Build Path Entries'. Click Next and select Maven Dependencies. This will publish the Maven dependency JAR files to the lib folder when Eclipse publishes your project to WST server.

This approach worked for me.

Solution 2

The easiest way to tell what Eclipse will put in your war file for server deployments is to do a right click -> Export -> WAR file. You can then uncompress this and see what's inside.

If you open up ".settings/org.eclipse.wst.common.component" you should see the definition of the web tools webapp, and see something like:

<project-modules id="moduleCoreId" project-version="1.5.0">
<wb-module deploy-name="MyApp">
    <wb-resource deploy-path="/" source-path="/src/main/webapp"/>
    <wb-resource deploy-path="/WEB-INF/classes" source-path="/src/main/java"/>
    <wb-resource deploy-path="/WEB-INF/classes" source-path="/src/main/resources"/>
    <property name="java-output-path" value="target/classes"/>
    <property name="context-root" value="MyApp"/>
</wb-module>
</project-modules>

On your build path in Eclipse, make sure "Maven Dependencies" appears in Libraries. The last versions of the m2eclipse plugin and WST should automatically pick up the Maven dependencies and drop them in WEB-INF/lib inside your war file.

Last but not least, you may want to consider picking up the latest copy of SpringSource Tool Suite (STS). STS has a number of good features, including bundling the latest m2eclipse and WST. You can still use it with Tomcat (or their version of Tomcat), and it has some archetypes and tutorials in there to get a webapp working quickly.

Enjoy!

Share:
67,268
Devanath Rao
Author by

Devanath Rao

Updated on May 22, 2020

Comments

  • Devanath Rao
    Devanath Rao almost 4 years

    I get the following exception when I try to start Tomcat through Eclipse (via right click on Project, Run As -> Run on Server).

    SEVERE: Error configuring application listener of class org.springframework.web.context.ContextLoaderListener
    java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener
        at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1676)
        at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1521)
        at org.apache.catalina.core.DefaultInstanceManager.loadClass(DefaultInstanceManager.java:415)
        at org.apache.catalina.core.DefaultInstanceManager.loadClassMaybePrivileged(DefaultInstanceManager.java:397)
        at org.apache.catalina.core.DefaultInstanceManager.newInstance(DefaultInstanceManager.java:118)
        at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4660)
        at org.apache.catalina.core.StandardContext$1.call(StandardContext.java:5226)
        at org.apache.catalina.core.StandardContext$1.call(StandardContext.java:5221)
        at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
        at java.util.concurrent.FutureTask.run(FutureTask.java:138)
        at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
        at java.lang.Thread.run(Thread.java:662)
    Jun 12, 2011 8:46:11 PM org.apache.catalina.core.StandardContext listenerStart
    

    I am building my project using Maven (m2eclipse). I have added the necessary dependencies to Spring libraries, and I can see the spring-web.jar file in the lib directory of the final artifact (which contains the class). When I deploy the application outside of IDE, it works fine.

    Any pointers as to why this happens would be highly appreciated.

  • Al Baker
    Al Baker almost 13 years
    I was wondering where that UI went in Eclipse -- used to be J2EE Module Dependencies or something along those lines, thanks!
  • Gleeb
    Gleeb almost 12 years
    hell yes! for all thouse that come here. this is the correct answer!
  • djb
    djb almost 12 years
    This is not working for me. I have a web project A which depends on B via pom.xml; "Maven Dependencies" is already listed in the Deployment Assembly, as is B. Note: the deploy path for B is "WEB-INF/lib/B-0.1.0-SNAPSHOT.jar", and it does not change depending on whether B is open in Eclipse or not. The Eclipse build path does change correctly (either the Eclipse project or the jar in my repo) to reflect whether the project is open or not. I'm using SpringSource Tool Suite 2.9.2.RELEASE. I still get a ClassNotFoundException at runtime even though the class resolves fine in Eclipse. Stumped.
  • Michael Wiles
    Michael Wiles almost 12 years
    @djb am having the same problem... don't know what to do
  • Anurag Kapur
    Anurag Kapur about 11 years
    Thanks for this! After spending a lot of frustrating time on this problem, this post was a treat to read.. worked like a charm!
  • AndreasEK
    AndreasEK about 11 years
    My Maven Dependencies are listed in the Deployment Assemblies. Still, from time to time, that exception occurs. The "rain dance" solution is to remove the war restart the server, and add it again. But I hope that there's a solution that makes this dance unnecessary. Any idea, why it may sometimes not work?
  • ptikobj
    ptikobj over 9 years
    @AndreasEK: I have a similar issue, from time to time, my maven dependencies get removed from the deployment assembly, and I have to re-add them with the method described in this answer... Is there a way to make this change persistent?
  • Yohan Liyanage
    Yohan Liyanage over 9 years
    @ptikobj One possibility is that if you have Maven POM auto-import enabled, any changes to POM may reset the deployment assembly.