Saturday, January 16, 2016

Solving java.lang.ClassNotFoundException: org.springframework.web.servlet.DispatcherServlet in Java Spring MVC Application

The  java.lang.ClassNotFoundException: org.springframework.web.servlet.DispatcherServlet error comes when you deploy a Spring MVC application into Tomcat or Jetty and Servlet container not able to found this class, which usually found in the spring-webmvc.jar file. The DispatcherServlet is the core of Spring MVC framework, it implements the FrontController pattern and intercept all HTTP requests sent to your Web application and subsequently route it to correct controller based upon URL mapping. You need to declare the DispatcherServlet in your 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.
You can see DispatcherServlet declaration in deployment descriptor or web.xml file as below:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="" xmlns="" xmlns:web="" xsi:schemaLocation="" id="WebApp_ID" version="3.0">
  <display-name>Spring MVC HelloWorld</display-name>


You can also read Spring in Action, 3rd Edition to learn more about how Spring MVC framework works and why it's better to use Spring MVC framework for developing Java Web application instead of just using Servlet, JSP and JSF. For now, this diagram gives you a good idea of an end to end the flow of how Spring MVC handles HTTP requests and what is the role of DispathcerServlet in Spring MVC based Java application.

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

When you 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.

If you are running your web application in Eclipse, you can tell Eclipse that it can find the Jars in additional location and not just WEB-INF/lib. Similarly, if you are running web application in tomcat, it also loads JAR file from tomcat/lib, but you must remember that when you unload or reload a web app, it unloads all classes loaded from JAR file found in WEB-INF/lib but it doesn't unload classes from JAR loaded from tomcat/lib so be careful with that, as multiple reload may throw java.lang.OutOfMemoryError: PermGen Space in Tomcat.

Here is how the stacktrace of this error look like:

java.lang.ClassNotFoundException: org.springframework.web.servlet.DispatcherServlet
    at org.apache.catalina.loader.WebappClassLoader.loadClass(
    at org.apache.catalina.loader.WebappClassLoader.loadClass(
    at org.apache.catalina.core.DefaultInstanceManager.loadClass(
    at org.apache.catalina.core.DefaultInstanceManager.loadClassMaybePrivileged(
    at org.apache.catalina.core.DefaultInstanceManager.newInstance(
    at org.apache.catalina.core.StandardWrapper.loadServlet(
    at org.apache.catalina.core.StandardWrapper.load(
    at org.apache.catalina.core.StandardContext.loadOnStartup(
    at org.apache.catalina.core.StandardContext.startInternal(
    at org.apache.catalina.util.LifecycleBase.start(
    at org.apache.catalina.core.ContainerBase.addChildInternal(
    at org.apache.catalina.core.ContainerBase.addChild(
    at org.apache.catalina.core.StandardHost.addChild(
    at org.apache.catalina.startup.HostConfig.deployDescriptor(
    at org.apache.catalina.startup.HostConfig$
    at java.util.concurrent.Executors$
    at java.util.concurrent.ThreadPoolExecutor.runWorker(
    at java.util.concurrent.ThreadPoolExecutor$

A solution of  java.lang.ClassNotFoundException: org.springframework.web.servlet.DispatcherServlet

The simple solution of this problem is to check if you the spring-mvc.jar in our classpath or not. Depending upon the spring MVC version e.g. 2.5, 3.0 or 4.0, this class is found in the spring-webmvc-2.5.2.jar file. Sometimes everything is also bundled in spring.jar, so don't forget to check that. If you have the project configured in Eclipse then you can also use the Eclipse shortcut Ctrl + T to find out which class file belong to which JAR file. A very useful trick while troubleshooting ClassNotFoundException and NoClassDefFoundError in Java.

That's all about how to solve java.lang.ClassNotFoundException: org.springframework.web.servlet.DispatcherServlet Error in Spring based Java Application. As I said, just check if you have spring-mvc.jar in your WEB-INF/lib directory or not. If no, then just add that JAR file and this error will go away.

Other troubleshooting guides for your reference:
  • How to solve java.lang.classnotfoundexception sun.jdbc.odbc.jdbcodbcdriver [solution]
  • Failed to read from SocketChannel: Connection reset by peer [fix]
  • Exception in thread "main" java.lang.ExceptionInInitializerError in Java Program [fix]
  • 2 ways to solve Unsupported major.minor version 51.0 error in Java [solutions]
  • Fixing Unsupported major.minor version 52.0 Error in Java [solution]
  • java.lang.ClassNotFoundException: [solution
  • How to solve java.lang.UnsatisfiedLinkError: no ocijdbc11 in Java [solution]
  • java.lang.ClassNotFoundException: org.postgresql.Driver [fix]
  • java.lang.ClassNotFoundException: org.springframework.web.servlet.DispatcherServlet [fix]
  • Map failed and java.lang.OutOfMemoryError: Map failed  [fix]
  • Cannot assign requested address: JVM_Bind [fix]
  • Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1 [solution]
  • org.hibernate.MappingException: Unknown entity Exception in Java [solution]
  • Too many files open [solution]

Further Reading
Spring Framework 5: Beginner to Guru
Spring Master Class - Beginner to Expert
Introduction to Spring MVC 4 By Bryan Hansen
Spring and Hibernate for Beginners

P.S. - If you want to learn how to develop RESTful Web Service using Spring MVC in depth, I suggest you join the REST with Spring certification class by Eugen Paraschiv. One of the best course to learn REST with Spring MVC. 

No comments :

Post a Comment