java.lang.ClassNotFoundException: org.Springframework.Web.Context.ContextLoaderListener [Solution]

Problem : You are getting java.lang.ClassNotFoundException : org.Springframework.Web.
Context.ContextLoaderListener in your Spring-based Java Web application.
Cause : This error comes when you are using Spring MVC framework in your Java Web application and configured org.springframework.web.context.ContextLoaderListener as a listener in your deployment descriptor also known as web.xml, but the JAR which contains this class is not available in web application's CLASSPATH. This is a very important class in Spring MVC framework, as it is used to load your spring configuration files e.g. applicatoin-Context.xml and others, defined in the context-param element of web.xml of your Java spring web application, as shown below :
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:application-Context.xml</param-value>
    </context-param>

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




Approach : Like any other ClassNotFoundException error, you first need to find out the JAR file on which this class is bundled and then add that JAR into appropriate place, e.g. WEB-INF/lib folder of your web application. So that the web application class loader e.g. org.apache.catalina.loader.WebappClassLoader can see this JAR file. You can see in below stack trace that it's the WebappClassLoader which failed to load this class :
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)


Solution :  It appears that the spring context loader listener class i.e. org.springframework.web.context.ContextLoaderListener  is moved to spring-web.jar since Spring 3.0. which means you should do following to fix java.lang.ClassNotFoundException : org.Springframework.Web.Context.ContextLoaderListener error :

1. If you are using Spring version 3.0 then add spring-web.jar into CLASSPATH i.e. just put it inside WEB-INF/lib folder.

2. If you are running in Spring 2.0 or lower version then just add spring.jar into your application's CLASSPATH, same just put it inside WEB-INF/lib directory.

3. If spring.jar or spring-web.jar is already in CLASSPATH then your problem is not due to JAR but due to wrongly configured classpath. See my post how to deal with ClassNotFoundException to troubleshoot  further.

4. If you are using Maven the add following dependency in your pom.xml file :
         <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>3.2.6.RELEASE</version>
        </dependency>


5. If you are getting java.lang.ClassNotFoundException : org.Springframework.Web.Context.ContextLoaderListener error In Eclipse and Tomcat, then you can also try following steps to make sure maven dependencies are in CLASSPATH and visible to Tomcat's web application class loader. All you need to do is add maven dependencies into your eclipse project's web deployment assembly.
  • Select Project, Right click and choose Properties.
  • Choose "Deployment Assembly".
  • Click the "Add..." button on the right side.
  • Choose "Java Build Path Entries" from the menu of directive type and click "Next".
  • Select "Maven Dependencies" from the Java Build Path Entries menu and click "Finish".
You will see maven dependencies added to the web deployment assembly definition by now.

6. If  you are getting java.lang.ClassNotFoundException : org.Springframework.Web.Context.ContextLoaderListener error in previously working project and you are sure that you not done anything which causes this error then you can try "Clean Tomcat Work Directory" or simply "Clean..". This is supposed to discard all published state and republish from scratch.

java.lang.ClassNotFoundException:org.Springframework.Web.Context.ContextLoaderListener [Solution]


That's all about how to solve java.lang.ClassNotFoundException : org.Springframework.Web.Context.ContextLoaderListener error in Spring based Java web application. Mostly it's the case of missing the spring-web.jar file as hardly anyone is running with spring 2.0 now, but it's good to remember that you need to add spring.jar into the class path for older spring version and spring-web.jar for newer spring version, starting from Spring 3.0.

Other common ClassNotFoundExceptions which comes in Java programs :
  • General Guide to solve java.lang.ClassNotFoundException in Java [guide]
  • How to solve java.lang.ClassNotFoundException: oracle.jdbc.driver.OracleDriver in Java? [solution]
  • How to solve java.lang.ClassNotFoundException:org.Springframework.Web.Context.ContextLoaderListener [solution]
  • How to solve java.lang.ClassNotFoundException: com.mysql.jdbc.Driver in Java MySQL? [solution]
  • java.lang.ClassNotFoundException: org.apache.commons.logging.LogFactory? [solution]
  • How to connect to MySQL database from Java Program [steps]
  • How to fix java.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.SQLServerDriver in Java? [solution]
  • How to fix java.lang.ClassNotFoundException: org.postgresql.Driver error in Java? [solution]

No comments:

Post a Comment