Saturday, January 26, 2013

Spring - java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener Quick Solution

If you have worked in Spring MVC than 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 how to resolve ClassNotFoundException in Java and we will discuss this in detail here. In this Java debugging tutorial, we will find some common reasons of java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener and quick fix or solution.

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

java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener  cause and solutionReal cause of this exception is that ClassLoader, which is loading this application (could be 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 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 on spring 2.0 or lower than check spring.jar in your classpath.

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

4) If its coming on Eclipse with Maven then check whether maven dependencies related to spring-web.jar  is included in your build path. If it’s included than just try to build the project using maven install, this will download all dependency. You can verify that whether relevant spring dependencies are included or not by checking Maven Dependencies in project node.

Solution of java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener

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

1) Include spring.jar if you are running on spring 2.0 or include spring-web.jar for spring 2.5,  3.0 and greater.
2) If you are running on Tomcat server than include above JAR in WEB-INF/lib folder
3) In case of Eclipse and maven include maven dependency on build path.
4) If you are running Tomcat inside Eclipse than cleaning Tomcat work directory and restarting server also helps. You can clean Tomcat work directory by right clicking Tomcat server node in Eclipse and selecting “Clean Work Directory” and “Clean” option.

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
How to resolve java.lang.UnsupportedClassVersionError with example

3 comments :

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

Keerthan reddi 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 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


Post a Comment