Preparing for Java and Spring Boot Interview?

Join my Newsletter, its FREE

Friday, May 10, 2024

How to Solve java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener in Spring? [Example]

If you have worked in Spring MVC then you may be familiar with  
java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener
which is common problem during deployment. Spring MVC throws  java.lang.ClassNotFoundException:                           org.springframework.web.context.ContextLoaderListener ,
when its not able to find org.springframework.web.context.ContextLoaderListener class which is used to load spring MVC configuration files like application-context.xml and other Spring Framework configuration files defined in context-param element of web.xml in an Spring MVC web application as:

<!-- spring mvc setup -->

I have touched base on this exception in my earlier article on how to resolve ClassNotFoundException in Java and we will discuss this in detail here. 

In this Java debugging and troubleshooting tutorial, we will find some common reasons for java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener and quick fix or solution. If you are struggling with this error then looking at these points can guide you on right direction. 

Cause of "java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener"

java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener  cause and solutionThe real cause of this exception is that ClassLoader, which is loading this application (could be a web-app class loader, if you are running on Tomcat server) is not able to find class "org.springframework.web.context.ContextLoaderListener" but manifestation and trigger could vary from one case to another. 

Here is a list of some possible causes of java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener in Spring MVC application :

1) If you are running with Spring 3.0 or greater and don't have spring-web.jar in your Classpath.

2) If you are in spring 2.0 or lower then check spring.jar in your classpath.

3) If spring.jar or spring-web.jar is in classpath then check your user running Java or Tomcat has permission to read those JAR files, possible cause in UNIX or Linux operation system.

4) If it's coming on Eclipse with Maven then check whether maven dependencies related to spring-web.jar  is included in your build path or not. 

If it’s included then just try to build the project using maven install, this will download all dependencies. 

You can further verify that whether relevant spring dependencies are included or not by checking Maven Dependencies in the project node. 

how to solve java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener

A solution of java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener

Here is the possible and quick solution to fix this exception in the Spring MVC application:

1) Include spring.jar if you are running on spring 5.0 or include spring-web.jar for spring 2.5,  3.0, and greater.

2) If you are running on a Tomcat server then include the above JAR in WEB-INF/lib folder

3) In the case of Eclipse and maven include maven dependency on the build path.

4) If you are running Tomcat inside Eclipse then cleaning the Tomcat work directory and restarting the server also helps. You can clean the Tomcat work directory by right-clicking the Tomcat server node in Eclipse and select the “Clean Work Directory” and “Clean” options.

These usually fix java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener in Spring MVC Application. If you still face issue than let us know and we will try to help you finding root cause of org.springframework.web.context.ContextLoaderListener and Fixing it.

Other Java debugging Tutorials from Javarevisited Blog


Jiri Pinkas said...

I usually put all Spring core dependencies to classpath even if I don't use all of them. This way I minimize these errors. Nice post how to do it using Maven:

Unknown said...

I am still facing error even though Maven dependencies has spring-web-3.2.0.RELEASE.jar in the build path and the jar is present in repository too. I did check the jar and I see the class file -'ContextLoaderListener' is also present.Still not sure as why I am getting this exception.Even my Dependency Assembly has Maven Dependencies too.
Please help me.
.WebApp logError SRVE0293E: [Servlet Error]-[Failed to load listener: org.springframework.web.context.ContextLoaderListener]: java.lang.ClassNotFoundException: class java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener

rajesh ranjan sethi said...

my error is this plz help me how i wil resolve it??

8:47:16 PM prepareRefresh
INFO: Refreshing display name []; startup date [Tue Jan 21 20:47:16 IST 2014]; root of context hierarchy
Jan 21, 2014 8:47:16 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from file [E:\Spring\SpringAnno1\.\src\DemoCfg.xml]
Jan 21, 2014 8:47:17 PM obtainFreshBeanFactory
INFO: Bean factory for application context []:
Jan 21, 2014 8:47:17 PM preInstantiateSingletons
INFO: Pre-instantiating singletons in defining beans [org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,tb,dt]; root of factory hierarchy
Jan 21, 2014 8:47:17 PM destroySingletons
INFO: Destroying singletons in defining beans [org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,tb,dt]; root of factory hierarchy
Exception in thread "main" org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [TestBean] for bean with name 'tb' defined in file [E:\Spring\SpringAnno1\.\src\DemoCfg.xml]; nested exception is java.lang.ClassNotFoundException: TestBean
at com.db.anno.TestClient.main(
Caused by: java.lang.ClassNotFoundException: TestBean
at Method)
at java.lang.ClassLoader.loadClass(
at sun.misc.Launcher$AppClassLoader.loadClass(
at java.lang.ClassLoader.loadClass(
at org.springframework.util.ClassUtils.forName(
... 8 more

Anonymous said...

I am getting to resolve this

Post a Comment