Problem :
You are getting java.lang.ClassNotFoundException : org.Springframework.Web.
Context.ContextLoaderListener in your Spring-based Java Web application.
Cause:
This error comes when you are using the Spring MVC framework in your Java Web application and configured org.springframework.web.context.ContextLoaderListener as a listener in your deployment descriptor also known as web.xml, but the JAR which contains this class is not available in the web application's CLASSPATH.
This is a very important class in the Spring MVC framework, as it is used to load your spring configuration files like applicatoin-Context.xml and others, defined in the context-param element of web.xml of your Java spring web application, as shown below :
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:application-Context.xml</param-value></context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
Approach
Like any other ClassNotFoundException error, you first need to find out the JAR file on which this class is bundled and then add that JAR into the appropriate place, like the WEB-INF/lib folder of your web application.
So that the web application class loader like org.apache.catalina.loader.WebappClassLoader can see this JAR file. You can see in below stack trace below that it's the WebappClassLoader that failed to load this class :
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:1676)
at org.apache.catalina.loader.WebappClassLoader
.loadClass(WebappClassLoader.java:1521)
at org.apache.catalina.core.DefaultInstanceManager
.loadClass(DefaultInstanceManager.java:415)
at org.apache.catalina.core.DefaultInstanceManager
.loadClassMaybePrivileged(DefaultInstanceManager.java:397)
at org.apache.catalina.core.DefaultInstanceManager
.newInstance(DefaultInstanceManager.java:118)
at org.apache.catalina.core.StandardContext.listenerStart(
StandardContext.java:4660)
at org.apache.catalina.core.StandardContext$1.call(
StandardContext.java:5226)
at org.apache.catalina.core.StandardContext$1.call(
StandardContext.java:5221)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
at java.util.concurrent.FutureTask.run(FutureTask.java:138)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(
ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(
ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:662)
Solution
It appears that the spring context loader listener class i.e. org.springframework.web.context.ContextLoaderListener is moved to spring-web.jar since Spring 3.0. which means you should do following to fix java.lang.ClassNotFoundException : org.Springframework.Web.Context.ContextLoaderListener error :
1. If you are using Spring version 3.0 then add spring-web.jar into CLASSPATH, I mean, just put it inside the WEB-INF/lib folder.
2. If you are running in Spring 2.0 or lower version then just add spring.jar into your application's CLASSPATH, the same just put it inside WEB-INF/lib directory.
3. If spring.jar or spring-web.jar is already in CLASSPATH then your problem is not due to JAR but due to a wrongly configured classpath. See my post on how to deal with ClassNotFoundException to troubleshoot further.
4. If you are using Maven then add the following dependency in your pom.xml file :
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>3.2.6.RELEASE</version>
</dependency>
5. If you are getting java.lang.ClassNotFoundException : org.Springframework.Web.Context.ContextLoaderListener error In Eclipse and Tomcat, then you can also try the following steps to make sure maven dependencies are in CLASSPATH and visible to Tomcat's web application class loader. All you need to do is add maven dependencies into your eclipse project's web deployment assembly.
Select Project, Right-click, and choose Properties.
Choose the "Deployment Assembly".
Click the "Add..." button on the right side.
Choose "Java Build Path Entries" from the menu of directive type and click "Next".
Select "Maven Dependencies" from the Java Build Path Entries menu and click "Finish".
You will see maven dependencies added to the web deployment assembly definition by now.
6. If you are getting java.lang.ClassNotFoundException : org.Springframework.Web.Context.ContextLoaderListener error in previously working project and you are sure that you not done anything which causes this error then you can try "Clean Tomcat Work Directory" or simply "Clean..". This is supposed to discard all published state and republish from scratch.
That's all about how to solve java.lang.ClassNotFoundException : org.Springframework.Web.Context.ContextLoaderListener error in Spring-based Java web application. Mostly it's the case of missing the spring-web.jar file as hardly anyone is running with spring 2.0 now, but it's good to remember that you need to add spring.jar into the classpath for the older spring version and spring-web.jar for the newer spring version, starting from Spring 3.0.
Source: Java67
The Tech Platform
Komentar