Saturday, March 30, 2013

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

While developing SpringMVC application in Eclipse, All require dependencies has been added to maven dependencies in pom.xml. Yet running application encountered with Exception java.lang.ClassNotFoundException: org.springframework.web.servlet.DispatcherServlet

Solution : 
Even though you have added all required dependencies into pom.xml for your J2EE WebProject you are require to reference maven dependencies to "Web Deployment Assembly"

Right Click J2EE Web Project->Deployment Assembly->Add->Select Maven Dependencies.




GuideLines and Troubleshootings :
In Order to run your project with Maven, It should be created with proper J2EE WebProject with Maven Guidelines as mentioned below.

1. Use Maven CommandLine Argument for WebProject :
mvn archetype:generate -DgroupId={project-packaging} -DartifactId={project-name} -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false
and Convert project to eclipse using mvn eclipse:eclipse

2. If you have installed M2E in Eclipse then create Maven Project and choose archetypeArtifactId as "maven-archetype-webapp". (if you don't prefer Maven command Line approch)

3. Once you have maven project, you will not be able to see Deployment Assembly in Project Properties. You need to add "Dynamic Web Module" from Project Facets in Project properties.
Here, You will be able to add "Maven Dependencies" references as mentioned above.

4. Since maven will only create src/main/webapp,src/main/resources folder. you are suppose to create java folder manually in src/main.

5. Project should contains web.xml in src/main/webapp/WEB-INF/

6. Folder src/main/webapp/ or src/main/webapp/WEB-INF can contains file like jsp,html,xml etc

7. In Run as->Maven Build, you can provide configurations like clean,install.

8. If you are facing issues like error: package javax.servlet.http does not exist", please add following dependencies to your pom.xml

<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>servlet-api</artifactId>
    <version>3.0-alpha-1</version>
</dependency>

9. If you are facing issue as [ERROR] Unable to locate the Javac Compiler in: [ERROR] C:\Program Files (x86)\Java\jre7\..\lib\tools.jar, Please follow(in Eclipse):
Window->Preferences->installed JRE->select path of JRE from JDK folder and not ourside JRE folder

10. Once Maven Build is successful, You are ready to run your application.
But If you are running application from Eclipse, In "Deploy Web Assembly" remove WebContent/ and add path for src/main/webapp since it's place where web.xml available.

I spent almost 3 hours to get my project working and finally "SpringMVC J2EE WebApplication with Maven Support working in WebLogic Server". All information shared above are valuable information and Unique.


Hope it might help you.

Author : Anuj Patel
Blog : http://goldenpackagebyanuj.blogspot.in/