Issue - java.lang.ClassNotFoundException: org.springframework.web.servlet.DispatcherServlet
Asked Answered
B

5

6

I am trying to get simple spring application up and running posted at Spring Example.

I am getting

SEVERE: Servlet /MavenWeb threw load() exception java.lang.ClassNotFoundException: org.springframework.web.servlet.DispatcherServlet
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1676)
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1521)
    at org.apache.catalina.core.DefaultInstanceManager.loadClass(DefaultInstanceManager.java:415)
    at org.apache.catalina.core.DefaultInstanceManager.loadClassMaybePrivileged(DefaultInstanceManager.java:397)
    at org.apache.catalina.core.DefaultInstanceManager.newInstance(DefaultInstanceManager.java:118)
    at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1073)
    at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1021)
    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)

I have spring.jar, spring-webmvc.jar and all other required jars in place and so am not sure why am getting this error.

Here is my web.xml file

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>Spring3-Hibernate</display-name>
  <welcome-file-list>
    <welcome-file>list.html</welcome-file>
  </welcome-file-list>
  <servlet>
    <servlet-name>spring</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>spring</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
</web-app>

and my Spring-servlet.xml file

<?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:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:jee="http://www.springframework.org/schema/jee" xmlns:lang="http://www.springframework.org/schema/lang"
    xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:util="http://www.springframework.org/schema/util"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd
        http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">

    <context:annotation-config />
    <context:component-scan base-package="net.viralpatel.contact" />

    <bean id="jspViewResolver"
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass"
            value="org.springframework.web.servlet.view.JstlView" />
        <property name="prefix" value="/WEB-INF/jsp/" />
        <property name="suffix" value=".jsp" />
    </bean>

    <bean id="messageSource"
        class="org.springframework.context.support.ReLoadableBundleMessageSource">
        <property name="basename" value="classpath:messages" />
        <property name="defaultEncoding" value="UTF-8" />
    </bean>

    <bean id="propertyConfigurer"
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
        p:location="/WEB-INF/jdbc.properties" />

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
        destroy-method="close" p:driverClassName="${jdbc.driverClassName}"
        p:url="${jdbc.databaseurl}" p:username="${jdbc.username}" p:password="${jdbc.password}" />

    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="configLocation">
            <value>classpath:hibernate.cfg.xml</value>
        </property>
        <property name="configurationClass">
            <value>org.hibernate.cfg.AnnotationConfiguration</value>
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">${jdbc.dialect}</prop>
                <prop key="hibernate.show_sql">true</prop>
            </props>
        </property>
    </bean>

    <tx:annotation-driven />
    <bean id="transactionManager"
        class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>

</beans>

I am not sure as to what is going wrong here and would appreciate any help or suggestions. I have gone through springsource forums and other similar questions on SO and still it is not working. Any suggestions as to what might be the road block here?

UPDATES:

DispatcherServlet Error is gone but now am getting another error

Stacktrace:

SEVERE: StandardWrapper.Throwable
java.lang.NoSuchFieldError: APPLICATION_CONTEXT_ID_PREFIX
    at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:430)
    at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:458)
    at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:339)
    at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:306)
    at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:127)
    at javax.servlet.GenericServlet.init(GenericServlet.java:160)
    at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1201)
    at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1114)
    at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1021)
    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)
Jul 18, 2011 11:25:09 AM org.apache.catalina.core.StandardContext loadOnStartup
SEVERE: Servlet /MavenWeb threw load() exception
java.lang.NoSuchFieldError: APPLICATION_CONTEXT_ID_PREFIX
    at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:430)
    at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:458)
    at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:339)
    at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:306)
    at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:127)
    at javax.servlet.GenericServlet.init(GenericServlet.java:160)
    at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1201)
    at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1114)
    at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1021)
    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)

Similar Question for Update Portion

Follow up Question on @Autowiring not working issue

Boothman answered 18/7, 2011 at 14:50 Comment(15)
Have you add the spring framework 3.0 mvc jar to the classpath?Knit
i have added it to WEB-INF/LIB folderBoothman
Please post the complete stacktrace. A ClassNotFoundException can sometimes mean that some other class is missing or that class initialization has failed ...Economical
@Stephan: Updated Complete Stack trace for referenceBoothman
What's Maven got to do with any of this? I see it in the stack trace.Peso
I am using Maven to pull jar files using pom.xml configuration.Boothman
WEB-INF/LIB and WEB-INF/lib are two different directories on a non-Windows machine (linux, unix, mac, etc).Hamelin
i was able to get rid of dispatcher servlet error but now have another one regarding java.lang.NoSuchFieldError, any clues about itBoothman
Just to be sure: Did you copy your jar file in the webapp on your Tomcat server?Scoutmaster
can you confirm the name of your "Spring-servlet.xml". According to specification it should be "spring-servlet.xml".Built
It can be anything you say it is; the name in the web.xml has to match that of the -servlet.xmlPeso
it should be [servlet-name]-servlet.xml by default if configuration file location is not providedBuilt
I am still working on the issue but I was able to resolve current errors but now am having issue with getting @Autowiring to work on fields, am getting : Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Boothman
Follow up question on https://mcmap.net/q/1087281/-cannot-locate-message-in-messagesource/164299Boothman
On a side note, never set <scope>compile</scope> in the pom for spring-webmvc.Mule
P
9

CNF exception should always make you think "I'm missing a JAR."

In this case, you want the Spring 3.0.5 JAR named org.springframework.web.servlet-3.0.5.RELEASE.jar. it contains the DispatcherServlet.

If you think they're in your CLASSPATH, perhaps you don't understand how to set it properly.

You should not have a CLASSPATH environment variable.

The Spring JARs and all their dependencies should be in your WEB-INF/lib.

Peso answered 18/7, 2011 at 14:53 Comment(16)
i have spring-webmvc-3.0.5.RELEASE.jar which contains DispatcherServlet but still am getting this issue.Boothman
The class loader disagrees with you.Peso
that is the issue am facing. what steps should i be doingBoothman
i have all my spring jars in web-inf/lib folderBoothman
are there any other steps which you would recommend to figure out as to why DispatcherServlet is not located by class loader?Boothman
You mean your spring-webmvc jar file contains an org.springframework.web.servlet.DispatcherServlet class?Scoutmaster
@Traroth - Yes, DispatcherServlet is in spring-webmvc.jarBoothman
Where is that JAR in your app right now? How is it that you refer to it in the CLASSPATH?Peso
@Boothman : It's a strange filename. Standard Spring jar files are not named that way. Are you using this file in your compilation environment? I suggest you download the official Spring release and use these files instead.Scoutmaster
Jar is in target/MavenWeb-0.0.1-SNAPSHOT/WEB-INF/lib/spring-webMVC.jar and in Libraries folder under MavenWeb/JavaResources/Libraries/spring-webMVC.jarBoothman
@duffymo: i was able to fix dispatcher servlet issue and now am getting another error SEVERE: StandardWrapper.Throwable java.lang.NoSuchFieldError: APPLICATION_CONTEXT_ID_PREFIX, have you ever experienced this error or what could be reasons for such an errorBoothman
I'd recommend getting Maven out of the picture.Peso
I cut & pasted the name of the JAR in my Spring 3.0.5 download that has DispatcherServlet in it. I'm certain mine is right; I can't speak for you or Maven.Peso
Google finds this Spring forum entry about the id prefix issue: forum.springsource.org/…. Get rid of Maven - it's killing you. Download the Spring 3.x stuff and put it in your WEB-INF/lib. It shouldn't need Maven to do that.Peso
check this out: #10047154Rothwell
Another reason why I hate Maven: it's an unnecessary complication that adds little or no value.Peso
H
3

If you're using Maven, you need this dependency:

<dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-webmvc</artifactId>
   <!-- Set this to the respective version -->
   <version>3.0.5.RELEASE</version>
</dependency>
Hamelin answered 18/7, 2011 at 15:13 Comment(1)
I am having this dependency only but still am getting Dispatcher Servlet IssueBoothman
H
1

This is a very old question

According to an article I read "Most probably the necessary Spring MVC related jar files are not loaded and deployed on tomcat startup. But note: these files are in your classpath and hence you are not getting any error in Eclipse IDE during development time. Happens only during runtime."

This is how I resolved mine:

  1. Right Click on Project.
  2. Choose Properties.
  3. Click Deployment Assembly.
  4. Click on Add.
  5. Select "Java Build Path Entries"
  6. Select Maven Dependencies and finish.

Clean the project and you are good to go.

Headroom answered 1/7, 2020 at 4:43 Comment(0)
T
0

Initially I was having the same error even though I configured the build path by adding external jars(In Eclipse Indigo). Later I copied the jar files directly to web-inf/lib and the problem disappeared.

Tampa answered 29/11, 2011 at 12:27 Comment(0)
K
0

I had a similar problem ... I asume that the spring-webmvc is a runtime dependency. The project can be compiled, but you get the runtime exception.

You can add it as runtime dependency in your pom, and I think it should work... (As compile time dependency it works, not sure if you want to add the dependency using the "compile" scope)

Keynesianism answered 9/4, 2013 at 13:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.