0

I researched and tried solutions in other questions regarding this problem, without success (such as the thread below)

java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener

I am a .NET developer, attempting to switch over to JAVA with my current project. I am configuring a Spring MVC project with Hibernate using JPA as my data repository layer (with Maven). I have configured the required files (web.xml, pom file, jpa context file, etc.). Once deploying it to the TomCat Apache server, however, I get an error stating that there are problems configuring the application listener.

Some steps I tried:

  1. My Spring Maven Dependencies are listed as compile rather than provided
  2. I have included Maven Dependencies in the build path
  3. I have updated the project using the Maven menu
  4. I checked to make sure spring web dependencies are included in the dependency hierarchy

Could any of you help me with this? Thank you in advance!

The full error:

SEVERE: Error configuring application listener of class org.springframework.web.context.contextLoaderListener java.lang.ClassNotFoundException: org.springframework.web.context.contextLoaderListener at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1702) at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1547) at org.apache.catalina.core.DefaultInstanceManager.loadClass(DefaultInstanceManager.java:532) at org.apache.catalina.core.DefaultInstanceManager.loadClassMaybePrivileged(DefaultInstanceManager.java:514) at org.apache.catalina.core.DefaultInstanceManager.newInstance(DefaultInstanceManager.java:142) at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4854) at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5434) at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150) at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559) at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549) at java.util.concurrent.FutureTask.run(Unknown Source) at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) at java.lang.Thread.run(Unknown Source)

It seems to be a fairly common error - however, the solutions presented thus far have not worked.

Below is my Pom file:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>mil.navy.navsupbsc.contacts</groupId>
    <artifactId>contacts</artifactId>
    <packaging>war</packaging>
    <version>0.0.1-SNAPSHOT</version>
    <name>contacts Maven Webapp</name>
    <url>http://maven.apache.org</url>
    <properties>

    <junit.version>4.11</junit.version>
    <spring.version>3.2.0.RELEASE</spring.version>
    <jackson.version>1.9.10</jackson.version>
    <hibernateValidator.version>4.2.0.Final</hibernateValidator.version>
    <commonsDbcp.version>1.4</commonsDbcp.version>
    <hibernateEntityManager.version>4.1.9.Final</hibernateEntityManager.version>
    <javaxTransactionJta.version>1.1</javaxTransactionJta.version>
</properties>
<dependencies>
    <!-- Added this dependency due to web search solutions for this problem -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>${junit.version}</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>commons-dbcp</groupId>
        <artifactId>commons-dbcp</artifactId>
        <version>${commonsDbcp.version}</version>
    </dependency>
    <!-- Spring 3 dependencies -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-validator</artifactId>
        <version>${hibernateValidator.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>${spring.version}</version>
        <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>${spring.version}</version>
        <scope>compile</scope>
    </dependency>
    <!-- Jackson JSON Mapper -->
    <dependency>
        <groupId>org.codehaus.jackson</groupId>
        <artifactId>jackson-mapper-asl</artifactId>
        <version>${jackson.version}</version>
    </dependency>

    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
        <version>1.2</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>servlet-api</artifactId>
        <version>2.5</version>
        <scope>provided</scope>
    </dependency>

    <!-- JPA Dependencies -->
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-entitymanager</artifactId>
        <version>${hibernateEntityManager.version}</version>
    </dependency>
    <dependency>
        <groupId>javax.transaction</groupId>
        <artifactId>jta</artifactId>
        <version>${javaxTransactionJta.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-jdbc</artifactId>
        <version>${spring.version}</version>
        <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-orm</artifactId>
        <version>${spring.version}</version>
        <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <!-- Oracle Dependencies -->
</dependencies>
<build>
    <finalName>contacts</finalName>
</build>
</project>

and my web.xml file:

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
<display-name>Archetype Created Web Application</display-name>

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:/jpaContext.xml</param-value>
</context-param>

<listener>
    <listener-class>org.springframework.web.context.contextLoaderListener</listener-class>
</listener>
<servlet>
    <servlet-name>Contact</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>Contact</servlet-name>
    <url-pattern>*.json</url-pattern>
</servlet-mapping>
    <servlet-mapping>
    <servlet-name>Contact</servlet-name>
    <url-pattern>*.html</url-pattern>
</servlet-mapping>

My persistence.xml file

<persistence xmlns="http://java.sun.com/xml/ns/persistence/"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence_2_0.xsd"
    version="2.0">

<persistence-unit name="punit">

</persistence-unit>

</persistence>

My jpa context file:

<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">

<context:annotation-config />

<bean
    class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />

<bean id="entityManagerFactory"
    class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="persistenceUnitName" value="punit" />
    <property name="dataSource" ref="dataSource" />
    <property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
            <property name="showSql" value="true" />
        </bean>
    </property>
    <property name="jpaPropertyMap">
        <map>
            <entry key="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect" />
            <entry key="hibernate.hbm2ddl.auto" value="none" />
            <entry key="hibernate.format_sql" value="true" />
        </map>
    </property>
</bean>

<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>

<tx:annotation-driven transaction-manager="transactionManager" />

<bean id="dataSource"
    class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="oracle.jdbc.OracleDriver" />
    <property name="url"
        value="jdbc:oracle:thin:@//###.###.##.##:####/xxxxx" />
    <property name="username" value="xxxxx" />
    <property name="password" value="xxxxx"/>
</bean>

Community
  • 1
  • 1
Laura Ritchey
  • 711
  • 8
  • 20

1 Answers1

3

I think you have a typo in your web.xml definition. It has to be

org.springframework.web.context.ContextLoaderListener

with a capital C

So basically, this..

<listener>
    <listener-class>org.springframework.web.context.contextLoaderListener</listener-class>
</listener>

has to be

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
Hrishikesh
  • 2,033
  • 1
  • 16
  • 26
  • Thanks so much! It looks like that fixed the error I mentioned (I'll mark yours as the answer very soon). In the meantime though - it added another error. I might need to create a new question for it since it is different - but maybe you'll know the answer to it? It is the error: Property 'driverClassName' threw exception; nested exception is java.lang.IllegalStateException: Could not load JDBC driver class [oracle.jdbc.OracleDriver] I added it as an external jar reference rather than as an external Maven repository. It is included in the build path. – Laura Ritchey Jan 16 '14 at 17:59
  • Why have you added it as an external jar though. The IllegalStateException does look like it is the jar file not present in the classpath, but why dont you add it as a Maven dependency? Wouldnt that be much more manageable? – Hrishikesh Jan 16 '14 at 18:12
  • Use this to find out the dependency that you need for ojdbc. http://search.maven.org/#search%7Cga%7C1%7Cojdbc – Hrishikesh Jan 16 '14 at 18:14
  • Honestly, I didn't use that because I was having trouble with using the local repositories. I wanted to use Ojdbc6 not ojdbc14 - and everything I read said it had to be installed in a local repository or using the mvn install. I did the MVN Install with the following command (mvn install:install-file -Dfile={C:\Users\zm09440\Downloads\ojdbc6.jar} -DgroupId=com.oracle -DartifactId=ojdbc6 -Dversion=11.2.0.3 -Dpackaging=jar -Dfile=ojdbc6.jar -DgeneratePom=true). Every time I do that, however, it does nothing. I'm probably missing a basic concept, but went the other way since I was stuck. – Laura Ritchey Jan 16 '14 at 19:23
  • well basically what maven install would do is the same as what would happen if you actually specified it as a dependency in a project. the maven install would forcibly pull the jar file from the external repository and put it in your .m2 folder(repository). which is in the C:\Users\\.m2 by default. You can add the ojdbc6 as a dependency by searching on mavenrepository.com and copy the snippet and add it in your pom. Right click your eclipse project, say Maven --> update dependencies and it should download it for you – Hrishikesh Jan 16 '14 at 19:27
  • Unfortunately ojdbc6 is not publicly available...otherwise I would do it that way. Apparently Oracle doesn't allow it to be included in the Maven Central Repository – Laura Ritchey Jan 16 '14 at 21:10
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/45405/discussion-between-laura-ritchey-and-hrishikesh) – Laura Ritchey Jan 16 '14 at 21:10
  • I figured out the problem...it turns out the Maven wasn't installed correctly, so the Install commands weren't working. I reinstalled it and used it from the external command line. The logging and console feedback confirmed that the install was successful (as well as showing in the m2 repository folder). I added the dependency and it all is working now. Thanks for your help! – Laura Ritchey Jan 17 '14 at 15:58