Preparing for Java and Spring Boot Interview?

Join my Newsletter, its FREE

Sunday, August 1, 2021

How to Fix java.lang.ClassNotFoundException: org.springframework.web.servlet.DispatcherServlet in Java Spring MVC? Solution

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 intercepts all HTTP requests sent to your Web application and subsequently routes it to the correct controller based upon URL mapping.

You need to declare the DispatcherServlet in your web.xml and specify the URL pattern so that a Servlet containers like Tomcat should send or route all HTTP requests to the configured DispatcherServlet.

We have also set the load-on-startup tag as 1 for this Servlet so that it should be loaded at the deployment time, rather than at the response time when a call for this Servlet comes.

In short, this DispatcherServlet is the link between the Servlet Container and Spring MVC framework.

You can further check out my recommend Spring MVC online courses to learn more about how Spring MVC framework works and why it's better to use the Spring MVC framework for developing Java Web applications instead of just using Servlet, JSP, and JSF.

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="" 
id="WebApp_ID" version="3.0">
  <display-name>Spring MVC HelloWorld</display-name>


For now, this diagram gives you a good idea of an end-to-end flow of how Spring MVC handles HTTP requests and what is the role of the Dispatcher servlet in Spring MVC based Java applications.

How to deal with java.lang.ClassNotFoundException: org.springframework.web.servlet.DispatcherServlet  in Java Spring MVC Application

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

When you deploy a Java web application that 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 locations 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 the 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$

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

The simplest solution to this problem is to check if you the spring-mvc.jar in our classpath or not.

Depending upon the Spring MVC version like 2.5, 3.0, 4.0, or 5.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 belongs 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]

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 courses to learn REST with Spring MVC.

No comments :

Post a Comment