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="http://www.w3.org/2001/XMLSchema-instance" 
xmlns="http://java.sun.com/xml/ns/javaee" 
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" 
id="WebApp_ID" version="3.0">
  <display-name>Spring MVC HelloWorld</display-name>

    <servlet>
    <servlet-name>main</servlet-name>
        <servlet-class>
             org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
        <load-on-startup>1</load-on-startup>
   </servlet>
    <servlet-mapping>
        <servlet-name>main</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>
</web-app>


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(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)






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]
  • java.net.SocketException: 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: com.microsoft.sqlserver.jdbc.SQLServerDriver [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]
  • java.io.IOException: Map failed and java.lang.OutOfMemoryError: Map failed  [fix]
  • java.net.BindException: 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]
  • java.net.SocketException: Too many files open java.io.IOException [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