[Solved] java.lang.ClassNotFoundException: org.Springframework.Web.Context.ContextLoaderListener in Java and Spring

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 the 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 the web application's CLASSPATH.

This is a very important class in the Spring MVC framework, as it is used to load your spring configuration files like 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>


Btw, if you are new to Spring core and Spring MVC then I also suggest you first go through a comprehensive Spring course like Spring Framework 5: Beginner to Guru course on Udemy. It's one of the most up-to-date courses to learn in Spring and covers Spring 5.0 and Reactive Programming as well. It's also very affordable and you can buy in just $9.9 on several Udemy sales.




What causes java.lang.ClassNotFoundException: org.Springframework.Web.Context.ContextLoaderListener?

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 the appropriate place, like the WEB-INF/lib folder of your web application.

So that the web application class loader like org.apache.catalina.loader.WebappClassLoader can see this JAR file. You can see in below stack trace below that it's the WebappClassLoader that 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)




2. How to solve java.lang.ClassNotFoundException: org.Springframework.Web.Context.ContextLoaderListener in Java 

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 the 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 mean, just put it inside the 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, the 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 a wrongly configured classpath. See my post on how to deal with ClassNotFoundException to troubleshoot further.

4. If you are using Maven then add the 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 the 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 the "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 states 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 classpath for the older spring version and spring-web.jar for the newer spring version, starting from Spring 3.0.

Further Learning
Spring MVC books and resources
Spring Web Application Developer Certification

Thanks for reading this article so far. If you like this interview question then please share it with your friends and colleagues. If you have any questions or suggestions then please drop a comment and I'll try to find an answer for you.

Other common ClassNotFoundExceptions that 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]

P. S. - If you want to learn Spring Framework from scratch, check out John Thomson's Spring Framework 5: Beginner to Guru course on Udemy. It's one of the most up-to-date courses to learn in Spring and covers Spring 5.0 and Reactive Programming as well. It's also very affordable and you can buy in just $9.9 on several Udemy sales.

7 comments:

  1. i have tried all the above steps but it doesn't works for me

    ReplyDelete
  2. i have tried all the above steps but it doesn't works for me

    ReplyDelete
  3. i have tried all the above steps but it doesn't works for me

    ReplyDelete
  4. i have tried all the above steps but it doesn't works for me

    ReplyDelete
    Replies
    1. what is the error you are getting? what have you been tried?

      Delete
  5. Thanks a lot for this blog. Adding spring-web dependency to pom.xml solved my problem.

    ReplyDelete

Feel free to comment, ask questions if you have any doubt.