satoshi
satoshi

Reputation: 4103

Running a Spring MVC application with Jetty gives "class path resource does not exist"

I'm new to Spring and I encountered a small problem: the web application runs perfectly when using Tomcat but has problem when running it with Jetty.

I run the following commands:

mvn package
java -jar target/dependency/jetty-runner.jar target/*.war

The error I get is:

org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [spring-config.xml]; nested exception is java.io.FileNotFoundException: class path resource [spring-config.xml] cannot be opened because it does not exist

Part of my pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>test</groupId>
    <artifactId>test</artifactId>
    <version>1.0</version>
    <packaging>war</packaging>
    <dependencies>
        ...
    </dependencies>
    <repositories>
        ...
    </repositories>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <version>2.3</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>copy</goal>
                        </goals>
                        <configuration>
                            <artifactItems>
                                <artifactItem>
                                    <groupId>org.mortbay.jetty</groupId>
                                    <artifactId>jetty-runner</artifactId>
                                    <version>7.4.5.v20110725</version>
                                    <destFileName>jetty-runner.jar</destFileName>
                                </artifactItem>
                            </artifactItems>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

Part of my web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
         http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
         version="2.5">
    ...
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            /WEB-INF/applicationContext.xml
        </param-value>
    </context-param>
    ...
</web-app>

Part of my /WEB-INF/applicationContext.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-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
    ...
    <import resource="classpath:spring-config.xml" />
</beans>

The relevant directories structure:

- src
--main
---java
----spring-config.xml
---webapp
----WEB-INF
-----applicationContext.xml
-----web.xml
-pom.xml

Seems to be a problem with the classpath definition but I don't know how to solve the problem. I already tried to specify the classpath with java -cp "..." ... or java -Djetty.class.path="..." ...

Any help is very appreciated!

Thank you.

Upvotes: 0

Views: 2558

Answers (2)

Dave Newton
Dave Newton

Reputation: 160171

spring-config.xml file should be in src/main/resources. XML files in the Java source directory won't be included on the classpath.

This is automatic if you use the jetty plugin and run with mvn jetty:run (or jetty:run-war).

Upvotes: 1

duffymo
duffymo

Reputation: 308743

Your CLASSPATH doesn't include the Spring context.

I'd advise you to package your app into a WAR and deploy that to Jetty. WEB-INF/classes is always in the CLASSPATH, so if you copy your Spring context XML to that directory the class loader will find them.

Do you need a ContextLoaderListener in your web.xml?

I see applicationContext.xml mentioned in your web.xml, but not spring-config.xml. That's the one the class loader is complaining about.

Upvotes: 1

Related Questions