File inside jar is not visible for spring

spring boot + .jar!/boot-inf/classes (no such file or directory)
classpath resource not found when running as jar
spring boot read file from resources
spring read file from resources
boot-inf/classes file not found
spring boot file not found exception
resource.getinputstream() to file
spring boot classpathresource jar

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?


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.

File inside jar is not visible for spring - spring - html, File inside jar is not visible for spring - spring. If your spring-context.xml and my. config files are in different jars then you will need to use classpath*:my.config? File inside jar is not visible for spring. 65. Read file from resources folder in Spring Boot. 12. Spring Boot - Reading Text File using ResourceLoader. 19.


I know this question has already been answered. However, for those using spring boot, this link helped me - https://smarterco.de/java-load-file-classpath-spring-boot/

However, the resourceLoader.getResource("classpath:file.txt").getFile(); was causing this problem and sbk's comment:

That's it. A java.io.File represents a file on the file system, in a directory structure. The Jar is a java.io.File. But anything within that file is beyond the reach of java.io.File. As far as java is concerned, until it is uncompressed, a class in jar file is no different than a word in a word document.

helped me understand why to use getInputStream() instead. It works for me now!

Thanks!

Reading files in resource path from jar artifact, The spring context is not contained within the method and the application try to use the absolute path to build a file by ( Files interface)… and fail :|. It's still not clear to me exactly where the properties and YAML files are. I can't tell if they are inside or outside the jar in each case. Without an accurate reproduction, we'll just be guessing about the problem and we don't have the time to do that.


In the spring jar package, I use new ClassPathResource(filename).getFile(), which throws theexception:

cannot be resolved to absolute file path because it does not reside in the file system: jar

But using new ClassPathResource(filename).getInputStream() will solve this problem. The reason is that the configuration file in the jar does not exist in the operating system's file tree,so must use getInputStream().

File inside jar is not visible for spring, Please use the below code of example, which help to parse json file inside jar in ( Spring Boot/Any Java Application/Jar) Example : JSONParser� Don’t use actuators if you can afford not to. Use Spring Boot 2.1 and Spring 5.1. Fix the location of the Spring Boot config file(s) with spring.config.location (command line argument or System property etc.). Switch off JMX - you probably don’t need it in a container - with spring.jmx.enabled=false. Run the JVM with -noverify.


I had similar problem when using Tomcat6.x and none of the advices I found was helping. At the end I deleted work folder (of Tomcat) and the problem gone.

I know it is illogical but for documentation purpose...

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 � To refer to Spring library modules in this guide we use a shorthand naming convention spring-* or spring-*.jar, where "*" represents the short name for the module (e.g. spring-core, spring-webmvc, spring-jms, etc.). The actual jar file name that you use may be in this form (see below) or it may not, and normally it also has a version number in


The answer by @sbk is the way we should do it in spring-boot environment (apart from @Value("${classpath*:})), in my opinion. But in my scenario it was not working if the execute from standalone jar..may be I did something wrong.

But this can be another way of doing this,

InputStream is = this.getClass().getClassLoader().getResourceAsStream(<relative path of the resource from resource directory>);

Spring cannot find file in classpath (within JAR) (Spring forum at , All files I try to get are located in the same JAR as the spring bean XML. Hidden way over there in the far right of the error message, three� The class in which you want to use @Autowired does not have a Spring annotation. Add one of the following annotatons to the class: @Component, @Repository, @Service, @Controller, @Configuration. They have different behaviors so choose carefully! Read more here. Instances created not by Spring. Autowired is cool! It makes certain things very easy.


6. Resources, File if the class path resource resides in the file system, but not for classpath resources The following two examples show how to force a ClassPathResource and a the classloader to load a file from within a jar on the classpath: getClass(). How to Externalize the Properties File. Example command line showing to pick up an external properties file: $ cat config/thin.properties dependencies.spring-boot-starter-web: org.springframework.boot:spring-boot-starter-web $ java -jar app.jar --thin.location=file:./config How to Create a Docker File System Layer. Precompute the dependencies:


Access a File from the Classpath using Spring, Learn how to easily open and read a file on the classpath using Spring's Resource API. expects the resource to be present in the filesystem and not within a jar file. In this quick article, we've seen a few ways to access and read a resource� First thing you have to make sure in the pom.xml file you should be able to see something like “ spring-boot-maven-plugin ”. This spring-boot-maven-plugin is responsible for creating a single


Load a File From Classpath In Spring Boot, As Spring works on the concept of fat jar it is good to know for developer the Let's See How We can read the File , We will now show you actual code to in spring boot if the file is present in the filesystem not inside a jar. Spring Security is smart enough to check that the user is logged in before checking to see if they are a member of the specified group. Let’s dig in to that Spring Expression Language above. Where is Spring Security looking for @roles.ADMIN? The @ symbol is special – it identifies a Java bean. In this case, a bean named roles.