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

By the way, if you are new to 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 course to learn modern Spring. It' also 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 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 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 Java web application.

In order to add Maven Dependency into deployment assembly in Eclipse, follow 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 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.

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
Spring in Action 4th edition by Craig Walls

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