Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring inside war cannot find classpath resource in an inner jar file

I have a project organized like this:

core
  -- /src/main/resources/company/config/spring-config.xml
webapp
  -- /WEB-INF/applicationContext.xml

The webapp depends on core.jar, which is included correctly in WEB-INF/lib when I deploy.

In web.xml I have:

<param-value>
    /WEB-INF/applicationContext.xml
</param-value>

And in applicationContext.xml I have:

<import resource="classpath:/company/config/spring-config.xml" />

But when I run, I get this error:

2012-10-04 20:03:39,156 [localhost-startStop-1] springframework.web.context.ContextLoader ERROR: Context initialization failed
org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Failed to import bean definitions from URL location [classpath:/company/config/spring-config.xml]
Offending resource: ServletContext resource [/WEB-INF/applicationContext.xml]; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [company/config/spring-config.xml]; nested exception is java.io.FileNotFoundException: class path resource [company/config/spring-config.xml] cannot be opened because it does not exist
    at org.springframework.beans.factory.parsing.FailFastProblemReporter.error(FailFastProblemReporter.java:68)
....
Caused by: java.io.FileNotFoundException: class path resource [company/config/spring-config.xml] cannot be opened because it does not exist
    at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:142)
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:336)
... 36 more

When spring-config.xml is in webapp, everything works fine.

I noticed the leading / is removed from some of the errors on the stack trace, and I wonder if this has anything to do with it.

Also, I am (unfortunately) using Spring 2.5, if that matters.

like image 930
JBCP Avatar asked Oct 05 '12 00:10

JBCP


People also ask

How can I get a resource folder from inside my JAR file?

What you could do is to use getResourceAsStream() method with the directory path, and the input Stream will have all the files name from that dir. After that you can concat the dir path with each file name and call getResourceAsStream for each file in a loop.

What is classpath resource in spring?

ClassPathResource is a Resource implementation for class path resources. It supports resolution as java. io. File if the class path resource resides in the file system, but not for resources in a JAR.

Where is the classpath in spring boot?

By default Spring Boot will serve static content from a directory called /static (or /public or /resources or /META-INF/resources) in the classpath.


1 Answers

For future reference, I figured out the problem after much debugging. It turns out Eclipse was building my "core" library as a jar, but with a web application package layout, so instead of my resource being located here:

/company/config/spring-config.xml

it was located here:

/WEB-INF/company/config/spring-config.xml

which caused the problem. I had checked the jar a few times before, but had never noticed the sneaky "/WEB-INF" hiding in plain sight.

Removing the project and re-importing it into Eclipse (via the Maven pom.xml file) was not enough to fix the problem.

I had to manually delete the Eclipse-specific .project, .classpath, and .settings files. When I did that and re-imported the project everything was fixed.

The moral of the lesson is: ALWAYS check your resource paths when the exception says "File Not Found".

like image 181
JBCP Avatar answered Oct 06 '22 05:10

JBCP