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 -->
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
        /WEB-INF/applicationContext.xml
        /WEB-INF/spring-security-setup.xml
    </param-value>
</context-param>
<listener>
   <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

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


4 comments:

  1. 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: http://blog.springsource.org/2009/12/02/obtaining-spring-3-artifacts-with-maven/

    ReplyDelete
  2. 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

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


    8:47:16 PM org.springframework.context.support.AbstractApplicationContext prepareRefresh
    INFO: Refreshing org.springframework.context.support.FileSystemXmlApplicationContext@e24e2a: display name [org.springframework.context.support.FileSystemXmlApplicationContext@e24e2a]; 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 org.springframework.context.support.AbstractApplicationContext obtainFreshBeanFactory
    INFO: Bean factory for application context [org.springframework.context.support.FileSystemXmlApplicationContext@e24e2a]: org.springframework.beans.factory.support.DefaultListableBeanFactory@1006d75
    Jan 21, 2014 8:47:17 PM org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
    INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@1006d75: 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 org.springframework.beans.factory.support.DefaultSingletonBeanRegistry destroySingletons
    INFO: Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@1006d75: 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 org.springframework.beans.factory.support.AbstractBeanFactory.resolveBeanClass(AbstractBeanFactory.java:1173)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.predictBeanType(AbstractAutowireCapableBeanFactory.java:479)
    at org.springframework.beans.factory.support.AbstractBeanFactory.isFactoryBean(AbstractBeanFactory.java:787)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:393)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:736)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:369)
    at org.springframework.context.support.FileSystemXmlApplicationContext.(FileSystemXmlApplicationContext.java:126)
    at org.springframework.context.support.FileSystemXmlApplicationContext.(FileSystemXmlApplicationContext.java:69)
    at com.db.anno.TestClient.main(TestClient.java:7)
    Caused by: java.lang.ClassNotFoundException: TestBean
    at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
    at org.springframework.util.ClassUtils.forName(ClassUtils.java:230)
    at org.springframework.beans.factory.support.AbstractBeanDefinition.resolveBeanClass(AbstractBeanDefinition.java:381)
    at org.springframework.beans.factory.support.AbstractBeanFactory.resolveBeanClass(AbstractBeanFactory.java:1170)
    ... 8 more


    ReplyDelete
  4. I am getting NoClassDefFoundError.how to resolve this

    ReplyDelete