[Solved] java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener

If you are working on a web application based on Spring that requires the use of org.springframework.web.context.ContextLoaderListener in the web.xml file and you are getting this exception when you start the server. This exception is more likely when you are working on Eclipse or possibly on other such IDEs.

The ContextLoaderListener class is used in the context of a web application and is part of the spring-web module.

1. The Exception

Exception log trace will look like this:

May 15, 2014 12:16:20 AM org.apache.catalina.core.StandardContext listenerStart
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:1714)
	at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1559)
	at org.apache.catalina.core.DefaultInstanceManager.loadClass(DefaultInstanceManager.java:532)
	at org.apache.catalina.core.DefaultInstanceManager.loadClassMaybePrivileged(DefaultInstanceManager.java:514)
	at org.apache.catalina.core.DefaultInstanceManager.newInstance(DefaultInstanceManager.java:133)
	at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4727)
	at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5285)
	at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
	at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559)
	at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549)
	at java.util.concurrent.FutureTask$Sync.innerRun(Unknown Source)
	at java.util.concurrent.FutureTask.run(Unknown Source)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
	at java.lang.Thread.run(Unknown Source)

May 15, 2014 12:16:20 AM org.apache.catalina.core.StandardContext listenerStart
SEVERE: Skipped installing application listeners due to previous error(s)
May 15, 2014 12:16:20 AM org.apache.catalina.core.StandardContext startInternal
SEVERE: Error listenerStart
May 15, 2014 12:16:20 AM org.apache.catalina.core.StandardContext startInternal
SEVERE: Context [/Spring4Struts2HibernateIntegration] startup failed due to previous errors
May 15, 2014 12:16:20 AM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-bio-8080"]
May 15, 2014 12:16:20 AM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["ajp-bio-8009"]
May 15, 2014 12:16:20 AM org.apache.catalina.startup.Catalina start
INFO: Server startup in 365 ms
exceptions-notes

2. Reasons

2.1. Missing Dependency of Spring-Web Module

Possibly you have not added the spring web dependencies to your project. Just add them if you have not.

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-web</artifactId>
    <version><!-- Use the version appropriate for your project --></version>
</dependency>

2.2. Missing Jars in Deployment Assembly

If you are still getting this exception then you must add these dependencies to project deployment assembly as well. Check the ‘WEB-INF/lib‘ directory of your deployed web application to ensure that the necessary Spring JAR files are present.

2.3. IDE Build Configurations

If you are using an IDE such as Eclipse or IntelliJ, try refreshing your project, cleaning the project, and rebuilding it to ensure that the IDE picks up the changes in the classpath.

Next, make sure you are adding the dependencies in the project deployment assembly as well.

To add dependencies in deployment assembly follow the below instructions.

1. Open project properties and select Choose Deployment Assembly. Then select the option “Java Build Path Entries“.

Deployment assembly option

2. Click Next and select all jar files. Click Finish.

Select all jars

3. Deployment assembly will look like this. Click Apply.

Apply deployment assembly

The above fixes will solve the issue.

Happy Learning !!

Comments

Subscribe
Notify of
guest
23 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.