Thursday, March 23, 2023

Eclipse - How to Fix java.lang.ClassNotFoundException: org.springframework.web.servlet.DispatcherServlet in Tomcat

Problem: You are getting java.lang.ClassNotFoundException exception complaining that Spring's DispatcherServlet class is not available in the classpath. This error is coming while running a Spring MVC based Java application from Eclipse and Tomcat as Server (running inside Eclipse IDE itself). You have either included spring framework JAR files manually by yourself or you are using Maven to download and manage dependent JAR files. Here is the stack trace of this error :

java.lang.ClassNotFoundException: org.springframework.web.servlet.DispatcherServlet
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1678)
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1523)
    at org.apache.catalina.core.DefaultInstanceManager.loadClass(DefaultInstanceManager.java:525)
    at org.apache.catalina.core.DefaultInstanceManager.loadClassMaybePrivileged(DefaultInstanceManager.java:507)
    at org.apache.catalina.core.DefaultInstanceManager.newInstance(DefaultInstanceManager.java:126)
    at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1099)
    at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1043)
    at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:4957


By the way, if you are new to the Spring framework then I also suggest you join a comprehensive and up-to-date course to learn Spring in depth. If you need recommendations, I highly suggest you take a look at Spring Framework 5: Beginner to Guru, one of the comprehensive and hands-on courses to learn modern Spring. It' also the most up-to-date and covers Spring 5.

It's also very affordable and you can buy in just $10 on Udemy sales which happen every now and then.


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

There are two main cause of getting java.lang.ClassNotFoundException: org.springframework.web.servlet.DispatcherServlet error in Eclipse :



1) First, either you don't have spring-webmvc-3.1.0.RELEASE.jar or similar JAR, depending upon which Spring framework version you are using e.g. Spring 4.0

2) Second is, Eclipse is not able to find this dependency in CLASSPATH.


But before going into those, let's first understand what is org.springframework.web.servlet.DispatcherServlet class? and why it is required?.

This class is actually the front controller in the Spring MVC framework, all HTTP requests go via this class to different controller classes. You define this class to map against a URL pattern in the web.xml file using a load-on-startup tag, that's why you see that loadOnStartup() call on stack trace of this error. 


This is why when you deploy your Spring MVC-based web application in Tomcat, the WebAppClassLoader search for org.springframework.web.servlet.DispatcherServlet class in class loader's CLASSPATH, which is WEB-INF/lib folder of your Java Web application.


If it doesn't found this class inside any JAR file in WEB-INF/lib, it throws java.lang.ClassNotFoundException: org.springframework.web.servlet.DispatcherServlet.


If you are new to Spring, you can also take a look at the Spring in Action book, one of the better books to learn the basics of Spring framework and Spring MVC together. The fourth edition of this book also covers Spring 4.

java.lang.ClassNotFoundException: org.springframework.web.servlet.DispatcherServlet in Tomcat and Eclipse


Now let's see how to solve this problem.



How to Fix java.lang.ClassNotFoundException: org.springframework.web.servlet.DispatcherServlet in Tomcat and Eclipse

Here are some steps you can take to deal with this error in Tomcat inside Eclipse IDE :

1) If you are using Maven and if spring-webmvc is not added as a dependency in your project's pom.xml then please add that. Once you do that, build your project using mvn install or mvn build. This will download spring MVC JAR files and another dependency.


2) The second step is to add Maven Dependency into deployment assembly. This is required to move all dependent JAR files into the WEB-INF/lib directory of the Java web application.

In order to add Maven Dependency into deployment assembly in Eclipse, follow the below steps :

i) Right-click your project, select properties, and choose Deployment Assembly.

How to add maven dependency in Eclipse Web project


ii)  Now click Add and choose Java Build Path Entries and select Maven dependency theirs. If your project is not set for the automatic build then just refresh the project and build it.

Now you should be able to run your Spring MVC application in the attached Tomcat from Eclipse IDE itself.

3) If this doesn't fix your problem then do a Maven clean, followed by Maven build to regenerate the WAR file.


That's all about how to fix java.lang.ClassNotFoundException: org.springframework.web.servlet.DispatcherServlet error in Eclipse and Tomcat. It's also possible that you get this error outside Eclipse IDE when you are deploying your WAR file in another environment. That time you must check whether a relevant JAR file like spring-webmvc-3.1.0.RELEASE.jar exists in either WEB-INF/lib directory of your application or Tomcat/lib directory.

It's better to keep in WEB-INF/lib directory because when you undeploy all the classes loaded by WebAppClassLoader are eligible for garbage collection, which is not the case for JAR files loaded from Tomcat's lib directory. That can cause a PermGen Memory leak in Tomcat.

Other Java and Spring Tutorial you may like
  • How Spring MVC works internally? (answer)
  • Spring Data JPA @Query Example (query example)
  • Spring Data JPA Repository (JpaReposistory example)
  • @SpringBootApplication vs @EnableAutoConfiguration? (answer)
  • 15 Spring Boot Interview Questions for Java Developers (questions)
  • Top 5 Courses to Learn and Master Spring Cloud (courses)
  • Top 5 Frameworks Java Developer Should Know (frameworks)
  • 10 Advanced Spring Boot Courses for Java developers (courses)
  • Top 5 Spring Cloud annotations Java programmer should learn (cloud)
  • Difference between @Component@Service, and @Controller in Spring (answer)
  • 5 Courses to learn Spring Cloud for Microservices (courses)
  • 20+ Spring MVC Interview Questions for Programmers (answer)
  • Difference between @Autowired and @Inject in Spring? (answer)
  • 5 Courses to Learn Spring Security for Java programmers (courses)
  • Top 5 Spring Boot Annotations Java Developers should know (read)
  • Difference between @RequestParam and @PathVariable in Spring (answer)
  • Top 7  Courses to learn Microservices in Java (courses)
  • 10 Spring MVC annotations Java developer should learn (annotations)

Thanks for reading this article so far. If you find my solution and explanation useful then please share it with your friends and colleagues. If you have any questions or feedback then please drop a note.

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