Spring – File inside jar is not visible for spring

classpathjarspring

All

I created a jar file with the following MANIFEST.MF inside:

Manifest-Version: 1.0
Ant-Version: Apache Ant 1.8.3
Created-By: 1.6.0_25-b06 (Sun Microsystems Inc.)
Main-Class: my.Main
Class-Path: . lib/spring-core-3.2.0.M2.jar lib/spring-beans-3.2.0.M2.jar

In its root there is a file called my.config which is referenced in my spring-context.xml like this:

<bean id="..." class="...">
    <property name="resource" value="classpath:my.config" />
</bean>

If I run the jar, everything looks fine escept the loading of that specific file:

Caused by: java.io.FileNotFoundException: class path resource [my.config] cannot be resolved to absolute file path because it does not reside in the file system: jar:file:/D:/work/my.jar!/my.config
        at org.springframework.util.ResourceUtils.getFile(ResourceUtils.java:205)
    at org.springframework.core.io.AbstractFileResolvingResource.getFile(AbstractFileResolvingResource.java:52)
    at eu.stepman.server.configuration.BeanConfigurationFactoryBean.getObject(BeanConfigurationFactoryBean.java:32)
    at eu.stepman.server.configuration.BeanConfigurationFactoryBean.getObject(BeanConfigurationFactoryBean.java:1)
    at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.doGetObjectFromFactoryBean(FactoryBeanRegistrySupport.java:142)
    ... 22 more
  • classes are loaded the from inside the jar
  • spring and other dependencies are loaded from separated jars
  • spring context is loaded (new ClassPathXmlApplicationContext("spring-context/applicationContext.xml"))
  • my.properties is loaded into PropertyPlaceholderConfigurer ("classpath:my.properties")
  • if I put my .config file outside the file system, and change the resource url to 'file:', everything seems to be fine…

Any tips?

Best Answer

If your spring-context.xml and my.config files are in different jars then you will need to use classpath*:my.config?

More info here

Also, make sure you are using resource.getInputStream() not resource.getFile() when loading from inside a jar file.