java.lang.ClassNotFoundException: org.springframework.web.servlet.DispatcherServlet

When we deploy a Spring MVC application into Tomcat and Servlet container not able to found this class org.springframework.web.servlet.DispatcherServlet then an Exception java.lang.ClassNotFoundException: org.springframework.web.servlet.DispatcherServlet error comes which usually found in the spring-webmvc.jar file.

The DispatcherServlet is the core of Spring MVC framework, In Spring’s web MVC framework the mechanism of dispatching the request to the appropriate controllers is achieved by configuring the DispatcherServlet class. DispatcherServlet is the class which manages the entire request handling process.Like a normal servlet DispatcherServlet also needs to be configured in the web deployement Descriptor(web.xml) and specify the URL pattern such that Servlet container like Tomcat should send all HTTP request to DispatcherServlet. We also set load-on-startup tag as 1 for this Servlet, so that it should be loaded at deployment time. This Servlet is the link between Servlet Container and Spring MVC framework.

DispatcherServlet in the deployment descriptor (web.xml)

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>2</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>*.html</url-pattern>
    </servlet-mapping>   
</web-app>

Reason of java.lang.ClassNotFoundException: org.springframework.web.servlet.DispatcherServlet

When we deploy a Java web application which uses Spring MVC into a web server like Tomcat, it reads the deployment descriptor (web.xml file) and starts looking for org.springframework.web.servlet.DispatcherServlet in its classpath. As per Java EE specification, all application dependency JARs must be in WEB-INF/lib directory, so if doesn’t find the JAR there it will throw the java.lang.ClassNotFoundException: org.springframework.web.servlet.DispatcherServlet.

Error stacktrace of this issue like:

java.lang.ClassNotFoundException: org.springframework.web.servlet.DispatcherServlet
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1702)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1547)
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:142)
at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1144)
at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1088)
at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:5176)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5460)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:901)
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:877)
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:633)
at org.apache.catalina.startup.HostConfig.deployDescriptor(HostConfig.java:663)
at org.apache.catalina.startup.HostConfig$DeployDescriptor.run(HostConfig.java:1642)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
at java.util.concurrent.FutureTask.run(FutureTask.java:262)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:744)

Solution for java.lang.ClassNotFoundException: org.springframework.web.servlet.DispatcherServlet

Solution 1- You did not include spring-beans and spring-context jars in your lib. If you are using maven (which will help a lot) those two lines will be enough

<dependency>
 <groupId>org.springframework</groupId>
  <artifactId>spring-context</artifactId>
   <version>4.1.0.RELEASE</version>
</dependency>
<dependency>
 <groupId>org.springframework</groupId>
  <artifactId>spring-webmvc</artifactId>
   <version>4.1.0.RELEASE</version>
</dependency>

Solution 2- The necessary jars are in your classpath but are not deployed on tomcat.

  • spring-beans – 4.1.0.RELEASE
  • spring-webmvc – 4.1.0.RELEASE
  • spring – 4.1.0.RELEASE

Previous
Next