96

i am using spring 3.1.0.RELEASE, and my servlet container is tomcat 7 and my IDE is eclipse indigo and the jar spring-webmvc-3.1.0.RELEASE.jar which contains the DispatcherServlet exists in the lib folder, and yet when running the application, i am getting the exception:

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)
    at org.apache.catalina.core.StandardContext$3.call(StandardContext.java:5284)
    at org.apache.catalina.core.StandardContext$3.call(StandardContext.java:5279)
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
    at java.util.concurrent.FutureTask.run(FutureTask.java:138)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
    at java.lang.Thread.run(Thread.java:662)

please advise why i am getting this exception, and how to fix it.

EDIT: following are my configuration files:

1- .springBeans:

<?xml version="1.0" encoding="UTF-8"?>
<beansProjectDescription>
    <version>1</version>
    <pluginVersion><![CDATA[2.9.0.201203011806-RELEASE]]></pluginVersion>
    <configSuffixes>
        <configSuffix><![CDATA[xml]]></configSuffix>
    </configSuffixes>
    <enableImports><![CDATA[false]]></enableImports>
    <configs>
        <config>src/main/webapp/WEB-INF/checkout-servlet.xml</config>
    </configs>
    <configSets>
    </configSets>
</beansProjectDescription>

2- web.xml:

<web-app>
  <display-name>Checkout</display-name>

  <servlet>
    <servlet-name>checkout</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>

  <servlet-mapping>
    <servlet-name>checkout</servlet-name>
    <url-pattern>*.action</url-pattern>
  </servlet-mapping>

</web-app>

3- checkout-servlet.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">

    <context:component-scan base-package="com.myapp"/>

    <bean id="myService" class="com.myapp.MyService"/>

</beans>

also when trying to access any page in the application, i get the exception:

HTTP Status 404 - Servlet checkout is not available

type Status report

message Servlet checkout is not available

description The requested resource (Servlet checkout is not available) is not available.
Apache Tomcat/7.0.22
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Mahmoud Saleh
  • 33,303
  • 119
  • 337
  • 498

18 Answers18

258

You need to add the "Maven Dependency" in the Deployment Assembly

  • right click on your project and choose properties.
  • click on Deployment Assembly.
  • click add
  • click on "Java Build Path Entries"
  • select Maven Dependencies"
  • click Finish.

Rebuild and deploy again

Note: This is also applicable for non maven project.

Vishrant
  • 15,456
  • 11
  • 71
  • 120
krakos
  • 2,581
  • 2
  • 12
  • 5
  • Nice - I was confused to see even after adding the proper dependency in my pom.xml didn't solved the error until I followed as above.. – srinivas Mar 10 '16 at 06:37
  • Nice.. perfect solution – Prasad V S Dec 01 '16 at 09:45
  • 1
    Why does this work? I mean, shouldn't all maven dependencies be available? If I was using the Tomcat7 pluging with tomcat7:run, how would this be accomplished? – Johnny Bigoode Mar 22 '17 at 15:32
  • Works like charm! – yrazlik Jul 12 '17 at 14:51
  • This is also applicable for non maven project.!! I created a maven project and i got same problem. this solution worked for me. – Bandham Manikanta Apr 01 '18 at 18:03
  • Before i add ContentNegotiatingViewResolver, the application worked properly. After i add that view resolver, i got this issue. Can somebody explain me the reason for getting this issue?? – Bandham Manikanta Apr 12 '18 at 01:52
  • this worked for me, but one thing i was not able to understand this problem occurred to me when i have change the project java version to 1.8 – Mateen May 09 '18 at 20:43
  • I am also facing the same issue in Eclipse IDE, I tried all these steps, but not able to resolve the issue. Please help! I am using Spring 6.0.0 with Tomcat 9. – ayush Jan 22 '23 at 06:08
38

Two possible answers:

1- You did not include spring-beans and spring-context jars in your lib. If you are using maven (which will help a lot) those two lines will be enough

<dependency>
 <groupId>org.springframework</groupId>
  <artifactId>spring-context</artifactId>
   <version>3.1.0.RELEASE</version>
</dependency>
<dependency>
 <groupId>org.springframework</groupId>
  <artifactId>spring-webmvc</artifactId>
   <version>3.1.0.RELEASE</version>
</dependency>

2- The necessary jars are in your classpath but are not deployed on tomcat.

Serkan Arıkuşu
  • 5,549
  • 5
  • 33
  • 50
20

I had the same problem with Idea Intellij and Spring 4. I fixed the problem and I wanted to share the answer with you.

I use tomcat 7 / idea intellij 13 / spring 4.

pom.xml dependencies:

  <properties>
    <spring.version>4.0.5.RELEASE</spring.version>
  </properties>

  <dependencies>     
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-core</artifactId>
      <version>${spring.version}</version>
    </dependency>

    <dependency>
enter code here
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>${spring.version}</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-jdbc</artifactId>
      <version>${spring.version}</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-web</artifactId>
      <version>${spring.version}</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>${spring.version}</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-test</artifactId>
      <version>${spring.version}</version>
      <scope>test</scope>
    </dependency>

    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>

    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-log4j12</artifactId>
      <version>1.7.2</version>
    </dependency>
        <dependency>
      <groupId>commons-dbcp</groupId>
      <artifactId>commons-dbcp</artifactId>
      <version>1.4</version>
    </dependency>
  </dependencies>

In idea Intellij, you need to go to File -> Project Settings -> Artifacts.

Then enter image description here

clean -> rebuild the project -> build the artifact, and everything will be ok.

peterh
  • 11,875
  • 18
  • 85
  • 108
Adelin
  • 18,144
  • 26
  • 115
  • 175
  • You're a genius. Could you explain what difference it makes? How come Maven imports everything, but you still need to do that so it works? – DMurta Nov 09 '15 at 22:58
  • @DMurta who said that maven exports everything? – Adelin Jul 01 '19 at 15:40
13

This solves the problem for me. It's easy and pretty simply explained.

Step 1

  • Right click on project
  • Click on Properties

Step 2

  • Click on Deployment Assembly Tab in the
  • Click Add...

Step 3

  • Click on Java Build Path Entries

Step 4

  • Click on Maven Dependencies
  • Click Finish button

Step 5

  • Redeploy Spring MVC application to Tomcat again
  • Restart Tomcat
  • List item

classnotfoundexception

Lycone
  • 650
  • 9
  • 18
11

i found that in the deployment assembly, there was the entry:

[persisted container] org.maven.ide.eclipse.maven2_classpath_container

i removed it, and added the maven dependencies entry, and it works fine now.

Mahmoud Saleh
  • 33,303
  • 119
  • 337
  • 498
6

If all of these advice doesn't work, you should re-create your Server (Tomcat or like that). That solved my problem.

Chinbold Gansukh
  • 1,809
  • 1
  • 15
  • 6
5

Include below dependency in your pom.xml

<dependency>
 <groupId>org.springframework</groupId>
  <artifactId>spring-webmvc</artifactId>
   <version>{spring-version}</version>
</dependency>
Yeikel
  • 854
  • 10
  • 18
Victor
  • 761
  • 8
  • 7
4

I was facing the same Issue. When I saw into maven repository .m2 folder(....m2\repository\org\springframework\spring-webmvc) in my local I found two 3.2.0.RELEASE folders. SO I removed one. Then I went to project, right click->properties->deployment essembly-> add maven dependencies. clean build and then restart the server. Then the DispatcherServlet got loaded.

TKS
  • 41
  • 4
  • Yes, this resolved my issue too, any idea why it is required to add dependencies in deployment assembly in eclipse ? – Derrick Dec 01 '16 at 07:16
4
  1. Go to properties of Project which your working
  2. Choose the Deployment Assembly menu and Click on add button
  3. opt the Java Build Path Entries and Click on Maven Dependencies
  4. Build the project and run

enter image description here

Lova Chittumuri
  • 2,994
  • 1
  • 30
  • 33
2

In my case I get this trouble after using the maven's update project utility. I tried all the workarounds you suggested but nothing seemed to work. At the end the solution was simply to remove the project from the server to ensure that it was clean, and add it again. Then it works, I hope this solution could help any of you.

user2947452
  • 91
  • 1
  • 1
2

You can use GlassFish server and the error will be resolved. I tried with tomcat7 and tomcat8 but this error was coming continuously but resolved with GlassFish. I think it's a problem with server.

These are the results with tomcat7: Results with tomcat7

Here are the results with GlassFish: Results with GlassFish

Marcos Delgado
  • 177
  • 3
  • 13
1

right click on your project and choose properties. click on Deployement Assembly. click add click on "Java Build Path Entries" select Maven Dependencies" click Finish.

1

It may be useful for someone, so I'll post it here. I was missing this dependency on my pom.xml

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <exclusions>
        <exclusion>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
        </exclusion>
    </exclusions>
</dependency>
luizfzs
  • 1,328
  • 2
  • 18
  • 34
0

I solved by following these steps:

  • Right click in the project > Configure > Convert to Maven project
  • After the conversion, right click in the project > Maven > Update project

This fixes the "Deployment Assembly" settings of the project.

leo
  • 1,243
  • 1
  • 18
  • 20
0

Move the jar files from your classpath to web-inf/lib, and run a new tomcat server.

ajai kumar
  • 59
  • 1
  • 8
0

I found a simple solution, Simply add your jars inside WEB-INF-->lib folder..

Karthik
  • 9
  • 5
0

I had this same issue in WebSphere, but couldn't find a solution even though I verified the Spring dependencies were there and it ran in tomcat just fine. I ended up uninstalling the application and was still getting the error so I think WebSphere was hanging onto some corrupt instance.

To fix the issue I had to reinstall the application, stop it, uninstall it and then reinstall it.

flip66
  • 341
  • 2
  • 5
  • 17
0

For me it was a mistake in the pom.xml - I'd set <scope>provided<scope> on my dependencies, and this was making them not get copied during the mvn package stage.

My symptoms were the error message the OP posted, and that the jars were not included in the WEB-INF/lib path inside the .war after package was run. When I removed the scope, the jars appeared in the output, and all loads up fine now.

Dan Rayson
  • 1,315
  • 1
  • 14
  • 37