Eclipse - Java.Lang.Classnotfoundexception: Org.Springframework.Web.Servlet.Dispatcherservlet Inwards Tomcat

Advertisement

Masukkan script iklan 970x90px

Eclipse - Java.Lang.Classnotfoundexception: Org.Springframework.Web.Servlet.Dispatcherservlet Inwards Tomcat

Sabtu, 18 Juli 2020

Problem : You are getting java.lang.ClassNotFoundException exception complaining that Spring's DispatcherServlet class is non available inwards the classpath. This fault is coming piece running a Spring MVC based Java application from Eclipse as well as Tomcat every bit Server (running within Eclipse IDE itself). You receive got either included boundary framework JAR files manually past times yourself or you lot are using Maven to download as well as create out subject JAR files. Here is the stack draw of this fault :

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



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

There are 2 principal stimulate of getting java.lang.ClassNotFoundException: org.springframework.web.servlet.DispatcherServlet fault inwards Eclipse :


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

2) Second is, Eclipse is non able to notice this dependency inwards CLASSPATH.


But earlier going into those, let's get-go understand
what is org.springframework.web.servlet.DispatcherServlet class? as well as why it is required?.

This degree is truly the forepart controller inwards Spring MVC framework, all HTTP requests become via this degree to unlike controller classes. You define this degree to map against a URL blueprint inwards the web.xml file using load-on-startup tag, that's why you lot run across that loadOnStartup() call on stack trace of this error. 


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


If it doesn't industrial plant life this degree within whatsoever JAR file inwards WEB-INF/lib, it throws java.lang.ClassNotFoundException: org.springframework.web.servlet.DispatcherServlet.


If you lot are novel to Spring, you lot tin laissez passer on the axe besides accept a hold off at Spring inwards Action book, 1 of the meliorate books to larn basics of Spring framework as well as Spring MVC together. The 4th edition of this mass besides covers Spring 4.

 This fault is coming piece running a Spring MVC based Java application from Eclipse as well as T Eclipse - java.lang.ClassNotFoundException: org.springframework.web.servlet.DispatcherServlet inwards Tomcat


Now let's run across how to solve this problem.



How to Fix java.lang.ClassNotFoundException: org.springframework.web.servlet.DispatcherServlet inwards Tomcat as well as Eclipse

Here are roughly steps you lot tin laissez passer on the axe accept to bargain amongst this fault inwards Tomcat within Eclipse IDE :

1) If you lot are using Maven as well as if spring-webmvc is non added every bit a dependency inwards your project's pom.xml as well as thus delight add together that. Once you lot produce that, create your projection using mvn install or mvn build. This volition download boundary MVC JAR files as well as roughly other dependency.


2) The mo measuring is to add together Maven Dependency into deployment assembly. This is required to motion all subject JAR files into WEB-INF/lib directory of Java spider web application.

In club to add together Maven Dependency into deployment assembly inwards Eclipse, follow below steps :

i) Right-click your project, select properties as well as direct Deployment Assembly.

 This fault is coming piece running a Spring MVC based Java application from Eclipse as well as T Eclipse - java.lang.ClassNotFoundException: org.springframework.web.servlet.DispatcherServlet inwards Tomcat


ii)  Now click Add as well as direct Java Build Path Entries as well as select Maven dependency theirs. If your projection is non laid for the automatic create as well as thus simply refresh the projection as well as create it.

Now you lot should endure able to run your Spring MVC application inwards attached Tomcat from Eclipse IDE itself.

3) If this doesn't gear upwards your work as well as thus produce a Maven clean, followed past times Maven create to regenerate WAR file.


That's all almost how to gear upwards java.lang.ClassNotFoundException: org.springframework.web.servlet.DispatcherServlet fault inwards Eclipse as well as Tomcat. It's besides possible that you lot acquire this fault exterior Eclipse IDE when you lot are deploying your WAR file inwards roughly other environment. That fourth dimension you lot must cheque whether relevant JAR file e.g. spring-webmvc-3.1.0.RELEASE.jar exists inwards either WEB-INF/lib directory of your application or Tomcat/lib directory. It's meliorate to perish on inwards WEB-INF/lib directory because when you lot undeploy all the classes loaded past times WebAppClassLoader are eligible for garbage collection, which is non the example for JAR files loaded from Tomcat's lib directory. That tin laissez passer on the axe stimulate PermGen Memory leak inwards Tomcat.

Further Reading
Spring Framework 5: Beginner to Guru
Spring Master Class - Beginner to Expert
Introduction to Spring MVC 4 By Bryan Hansen
Spring as well as Hibernate for Beginners
Spring inwards Action 4th edition past times Craig Walls

P.S. - If you lot desire to larn how to prepare RESTful Web Service using Spring MVC inwards depth, I advise you lot bring together the REST amongst Spring certification class past times Eugen Paraschiv. One of the best course of teaching to larn REST amongst Spring MVC.