Friday, August 21, 2009

Spring ContextLoaderListener ClassNotFoundException

I ran a Java web application with Maven, Tomcat in Eclipse. When the application started, Eclipse console has the exception:
java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener

Of course, I did not forget to define Spring dependency in Maven pom.xml. But when I checked the deployment at
ECLIPSE_WORKSPACE\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\APPLICATION\WEB-INF\lib
, there was no Jar indeed. So it looked like something went wrong with Eclipse/Tomcat/Maven deployment.

I did a couple of things to make it work.
1. Get rid of duplicated classes. For example, the dependency on spring-aspects also brings in spring-beans and spring-core, which are already part of spring dependency. Tip: use "mvn dependency:tree" to the dependency tree. So I need to exclcude it by
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>2.5.6</version>
<!-- exclude spring-beans which is part of spring-2.5.6.jar -->
<exclusions>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
</exclusion>
</exclusions>
</dependency>
2. Right click project > Maven > Update Project Configuration. Disable and re-enable Maven dependency management if necessary.

Now I found all the required jars in the right place. I started the application and no error!

This seems to be a glitch with Eclipse Maven plugin. I need to keep an eye on it.

4 comments:

  1. Hi,
    I'm building without a Maven plugin and getting the exact same error. In other words, this has more to do with a Eclipse .classpath bug than with Maven or a Maven plugin.
    Regards,
    Stewart

    ReplyDelete
  2. I seem to run into this prob every time I use pom editor in eclipse to update the dependencies. I just resolved the issue by following your disable/re-enable Maven Dep Mgmt suggestion.

    Thanks.

    ReplyDelete
  3. I find I run into this problem from time to time, and the solution is to manually update the .classpath file for your project.
    Find the classpath entry for the path org.maven.ide.eclipse.MAVEN2_CLASSPATH_CONTAINER, and add the following to it:

    ReplyDelete
  4. oops..lost the actual code... this is what to add:
    <attributes>
    <attribute name="org.eclipse.jst.component.dependency" value="/WEB-INF/lib"/>
    </attributes>

    ReplyDelete