How to Solve java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener in Spring? [Example] (original) (raw)
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.xmland other Spring Framework configuration files defined in context-param element of web.xml in an Spring MVC web application as:
<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>
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"
The 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 :
If you are running with Spring 3.0 or greater and don't have spring-web.jar in your Classpath.
If you are in spring 2.0 or lower then check spring.jar in your classpath.
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.
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.
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:
Include spring.jar if you are running on spring 5.0 or include spring-web.jar for spring 2.5, 3.0, and greater.
If you are running on a Tomcat server then include the above JAR in WEB-INF/lib folder
In the case of Eclipse and maven include maven dependency on the build path.
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
- Difference between NoClassDefFoundError and ClassNotFoundException in Java
- How to fix java.lang.ClassNotFoundException: com.mysql.jdbc.Driver in JDBC
- How to fix Failed to load Main-Class manifest attribute from a jar
- How to fix java.io.NotSerializableException: org.apache.log4j.Logger Error in Java
- How to solve java.util.NoSuchElementException in Java
- How to resolve java.lang.UnsupportedClassVersionError with example