0

Im working on a project with spring boot. After finish this project and package with mvn install in order to get my compiled jar file, i get some issue during the exploitation.

Some controller on my app need to get files or file path from classpath by using

String filePath = ResourceUtils.getFile("classpath:file.json").getPath()

this method work until i package to jar

Same issue to access properties files from classpath.

James Z
  • 12,209
  • 10
  • 24
  • 44
  • Does this answer your question? [IOException when reading JSON file from resources in Spring Boot](https://stackoverflow.com/questions/58703834/ioexception-when-reading-json-file-from-resources-in-spring-boot) – Ryuzaki L Mar 14 '20 at 21:35

1 Answers1

0

You are correct that your approach will work from an IDE but for the jar it won't find the file. So use code like below to get it worked.

File file = new ClassPathResource("file.json").getFile();

Here are some different ways to achieve the same.

Alien
  • 15,141
  • 6
  • 37
  • 57
  • thank for your help. but this method work also only in my IDE. after package to the jar, i still get issue. java.io.FileNotFoundException: class path resource [file.json] cannot be resolved to absolute file path because it does not reside in the file system: jar:file:/Desktop/myapp/target/myapp-0.0.1-SNAPSHOT.war!/WEB-INF/classes!/file.json – SkalpoVich Mar 16 '20 at 20:18