1

I am using the google's common IO, library to read the files in my resources folder of Spring boot app. its working perfectly fine in the IntelliJ, but when I am running this from the command line, My app is picking the application.yml present in the same resources folder but not picking other files in the same folder.

I've checked that files are present in the jar file, as shown below

unzip -l my.jar | grep ".json"

 983  04-14-2022 16:16   BOOT-INF/classes/abc.json
 423  04-14-2022 16:16   BOOT-INF/classes/xyz.json

But App is throwing following File not found exception, with the same location.

java.io.FileNotFoundException: file:/Users/amit/code/so-search/target/my.jar!/BOOT-INF/classes!/abc.json (No such file or directory)

This is how I am reading the files in my code.

public static FileReader getFileReader(String fileName) throws FileNotFoundException {
        File file = new File(Resources.getResource(fileName).getFile());
        return new FileReader(file);
    }
Amit
  • 30,756
  • 6
  • 57
  • 88
  • 1
    I'm not exactly sure, but isn't the meaning of a `!` in the path, that it is trying to open a zip/jar archive? In the exception you sent, it seems like it is trying to open `classes` as a zip archive (`classes!`). Yet in the console output you sent it looks like it's just a normal folder. Actually yeah, it seems so: https://stackoverflow.com/a/9530575/4541480. Don't exactly know why the library is returning such a path, but maybe that's the cause for it not working? – Nicofisi Apr 14 '22 at 11:38
  • @Nicofisi , yeah its weird, gone through multiple articles but they didn't help :(, have done same thing million times but not in sprint boot, did same in DW – Amit Apr 14 '22 at 12:01
  • 2
    hope this can answer your question: https://stackoverflow.com/questions/25869428/classpath-resource-not-found-when-running-as-jar basically you should read from an input stream instead from directly the file in the resource folder – Johannes Apr 14 '22 at 12:15
  • 1
    The double `!` is weird, but the more fundamental issue is that the `!` syntax has no meaning outside of a `jar` URL. Calling `getFile()` doesn’t magically turn the resource URL (which would have been a `jar` URL) into a file URL - it just returns the path component of the URL (plus the query string if there is one), which isn’t going to make sense if treated as a file name. – rimesc Apr 14 '22 at 12:22
  • @Johannes, thanks a lot, your link helped fixed me the issue – Amit Apr 15 '22 at 06:49
  • @Johannes, let me know if you want to write a answer, would be helpful for others – Amit Apr 20 '22 at 05:08

0 Answers0