Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

File inside jar is not visible for spring

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?

like image 929
BTakacs Avatar asked Feb 14 '13 14:02

BTakacs


People also ask

How do I access files inside a jar?

You can use getResource() to obtain a URL for a file on the classpath, or getResourceAsStream() to get an InputStream instead. Show activity on this post. You could read the contents of a JAR file using the JarFile class.

Where can I find the jar file in spring boot?

Index Files. Spring Boot Loader-compatible jar and war archives can include additional index files under the BOOT-INF/ directory. A classpath. idx file can be provided for both jars and wars, and it provides the ordering that jars should be added to the classpath.


1 Answers

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.

like image 100
sbk Avatar answered Sep 29 '22 10:09

sbk