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

Problem : You are getting java.lang.ClassNotFoundException exception complaining that Spring's DispatcherServlet class is non available inwards the classpath. This fault is coming spell running a Spring MVC based Java application from Eclipse in addition to Tomcat equally Server (running within Eclipse IDE itself). You receive got either included jump framework JAR files manually past times yourself or you lot are using Maven to download in addition to cope theme JAR files. Here is the stack line 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 induce 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 similar 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 outset understand
what is org.springframework.web.servlet.DispatcherServlet class? in addition to why it is required?.

This shape is truly the front end controller inwards Spring MVC framework, all HTTP requests acquire via this shape to unlike controller classes. You define this shape to map against a URL pattern 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 shape inwards shape loader's CLASSPATH, which is WEB-INF/lib folder of your Java Web application.


If it doesn't industrial plant life this shape 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 likewise receive got a hold off at Spring inwards Action book, i of the meliorate books to acquire basics of Spring framework in addition to Spring MVC together. The quaternary edition of this majority likewise covers Spring 4.

 This fault is coming spell running a Spring MVC based Java application from Eclipse in addition to 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 in addition to Eclipse

Here are roughly steps you lot tin receive got to bargain alongside this fault inwards Tomcat within Eclipse IDE :

1) If you lot are using Maven in addition to if spring-webmvc is non added equally a dependency inwards your project's pom.xml in addition to thence delight add together that. Once you lot produce that, construct your projection using mvn install or mvn build. This volition download jump MVC JAR files in addition to roughly other dependency.


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

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

i) Right-click your project, select properties in addition to pick out Deployment Assembly.

 This fault is coming spell running a Spring MVC based Java application from Eclipse in addition to T Eclipse - java.lang.ClassNotFoundException: org.springframework.web.servlet.DispatcherServlet inwards Tomcat


ii)  Now click Add in addition to pick out Java Build Path Entries in addition to select Maven dependency theirs. If your projection is non laid upwards for the automatic construct in addition to thence only refresh the projection in addition to construct it.

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

3) If this doesn't cook your occupation in addition to thence produce a Maven clean, followed past times Maven construct to regenerate WAR file.


That's all well-nigh how to cook java.lang.ClassNotFoundException: org.springframework.web.servlet.DispatcherServlet fault inwards Eclipse in addition to Tomcat. It's likewise 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 banking concern tally 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 conk along 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 illustration for JAR files loaded from Tomcat's lib directory. That tin induce 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 in addition to Hibernate for Beginners
Spring inwards Action 4th edition past times Craig Walls

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

Komentar

Postingan populer dari blog ini

Common Multi-Threading Mistakes Inwards Coffee - Calling Run() Instead Of Start()

3 Examples Of Parsing Html File Inwards Coffee Using Jsoup

Why You Lot Should Command Visibility Of Shape Too Interface Inward Java