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

Community
  • 1
  • 1
Rachel
  • 100,387
  • 116
  • 269
  • 365
  • Have you add the spring framework 3.0 mvc jar to the classpath? – NiL Jul 18 '11 at 14:53
  • i have added it to WEB-INF/LIB folder – Rachel Jul 18 '11 at 14:56
  • Please post the complete stacktrace. A `ClassNotFoundException` can sometimes mean that some other class is missing or that class initialization has failed ... – Stephen C Jul 18 '11 at 15:01
  • @Stephan: Updated Complete Stack trace for reference – Rachel Jul 18 '11 at 15:03
  • What's Maven got to do with any of this? I see it in the stack trace. – duffymo Jul 18 '11 at 15:11
  • I am using Maven to pull jar files using pom.xml configuration. – Rachel Jul 18 '11 at 15:15
  • `WEB-INF/LIB` and `WEB-INF/lib` are two different directories on a non-Windows machine (linux, unix, mac, etc). – carlspring Jul 18 '11 at 15:30
  • i was able to get rid of dispatcher servlet error but now have another one regarding `java.lang.NoSuchFieldError`, any clues about it – Rachel Jul 18 '11 at 15:31
  • Just to be sure: Did you copy your jar file in the webapp on your Tomcat server? – Alexis Dufrenoy Jul 18 '11 at 16:21
  • can you confirm the name of your "Spring-servlet.xml". According to specification it should be "spring-servlet.xml". – Kowser Jul 18 '11 at 16:39
  • It can be anything you say it is; the name in the web.xml has to match that of the -servlet.xml – duffymo Jul 18 '11 at 16:58
  • it should be [servlet-name]-servlet.xml by default if configuration file location is not provided – Kowser Jul 18 '11 at 19:08
  • 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: ` – Rachel Jul 18 '11 at 20:14
  • Follow up question on http://stackoverflow.com/q/6748939/164299 – Rachel Jul 19 '11 at 14:40
  • On a side note, never set `compile` in the pom for spring-webmvc. – whirlwin Jan 27 '12 at 09:51

5 Answers5

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.

duffymo
  • 305,152
  • 44
  • 369
  • 561
  • i have `spring-webmvc-3.0.5.RELEASE.jar` which contains `DispatcherServlet` but still am getting this issue. – Rachel Jul 18 '11 at 14:55
  • 4
    The class loader disagrees with you. – duffymo Jul 18 '11 at 14:56
  • that is the issue am facing. what steps should i be doing – Rachel Jul 18 '11 at 14:57
  • i have all my spring jars in web-inf/lib folder – Rachel Jul 18 '11 at 14:57
  • are there any other steps which you would recommend to figure out as to why DispatcherServlet is not located by class loader? – Rachel Jul 18 '11 at 15:01
  • You mean your spring-webmvc jar file contains an org.springframework.web.servlet.DispatcherServlet class? – Alexis Dufrenoy Jul 18 '11 at 15:07
  • @Traroth - Yes, DispatcherServlet is in spring-webmvc.jar – Rachel Jul 18 '11 at 15:09
  • Where is that JAR in your app right now? How is it that you refer to it in the CLASSPATH? – duffymo Jul 18 '11 at 15:10
  • @Rachel : 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. – Alexis Dufrenoy Jul 18 '11 at 15:13
  • 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.jar – Rachel Jul 18 '11 at 15:14
  • @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 error – Rachel Jul 18 '11 at 15:34
  • 2
    I'd recommend getting Maven out of the picture. – duffymo Jul 18 '11 at 16:09
  • 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. – duffymo Jul 18 '11 at 16:10
  • Google finds this Spring forum entry about the id prefix issue: http://forum.springsource.org/showthread.php?79929-java.lang.NoSuchFieldError-APPLICATION_CONTEXT_ID_PREFIX. 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. – duffymo Jul 18 '11 at 16:18
  • 1
    check this out: http://stackoverflow.com/questions/10046654/maven-spring-dynamic-web-module-eclipse-java-lang-classnotfoundexcepti/12600686#12600686 – Adriano Oct 03 '12 at 14:29
  • Another reason why I hate Maven: it's an unnecessary complication that adds little or no value. – duffymo Oct 03 '12 at 15:32
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>
carlspring
  • 31,231
  • 29
  • 115
  • 197
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)

Sergiu
  • 266
  • 3
  • 3
0

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.

Shivakumar N.R
  • 170
  • 1
  • 12
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.

Kate Gregory
  • 18,808
  • 8
  • 56
  • 85
Balaswamy Vaddeman
  • 8,360
  • 3
  • 30
  • 40