5

I got following error after export Spring Boot version 1.5.7.RELEASE to runnable JAR. I don't use maven because security reasons, and I added all JARs in build path.

I run below command

java -jar mailer.jar

then I received error as you see in screenshot

enter image description here

Vy Do
  • 46,709
  • 59
  • 215
  • 313
Salah Atwa
  • 1,648
  • 2
  • 12
  • 13
  • 1
    Please post this as text – Scary Wombat Nov 07 '18 at 06:28
  • 1
    https://stackoverflow.com/questions/26185137/spring-boot-resource-not-found-when-using-executeable-jar/26186317 might help you. – Alien Nov 07 '18 at 06:29
  • 2018-11-06 16:54:52.234 WARN 208 --- [ main] .i.s.PathMatchingResourcePatternResolver : Cannot search for matching files underneath URL [rsrc:com/atwa/base/] because it does not correspond to a directory in the file system java.io.FileNotFoundException: URL [rsrc:com/atwa/base/] cannot be resolved to absolute file path because it does not reside in the file system: rsrc:com/atwa/base/ – Salah Atwa Nov 07 '18 at 06:34
  • 1
    can you update project structure and error message in post – Ryuzaki L Nov 07 '18 at 06:45
  • Please add line 22 (and surrounding lines) of your Applicaiton.java to the question. – DodgyCodeException Nov 07 '18 at 07:39

3 Answers3

8

Because when your resource does not exist in packaged uber-jar, has problem with classpath. Use solution like this

String fuu = "";
ClassPathResource classPathResource = new ClassPathResource("static/foo.txt");
try {
    byte[] binaryData = FileCopyUtils.copyToByteArray(classPathResource.getInputStream());
    fuu = new String(binaryData, StandardCharsets.UTF_8);
} catch (IOException e) {
    e.printStackTrace();
}
Vy Do
  • 46,709
  • 59
  • 215
  • 313
  • it's look greate , but i need to export runnable jar , that i can run with the following comman java -jar myJar.jar i face the problem in screen shot – Salah Atwa Nov 07 '18 at 06:53
  • 1
    Hi @James, I need to create a new file while is dockerized, do you have any solution for that, I did a question on my account, if you want can you answer me – Sonn Aug 27 '22 at 17:49
4

It seems like the application is trying to access a file through the AbstractFileResolvingResource.getFile() (a couple of rows down in the stack trace) which is not possible from a runnable spring boot jar (it may work when running from an IDE).

Try using getInputStream() instead, see for example this post.

nordenvall
  • 188
  • 6
4

Try this

IOUtils.toString(new InputStreamReader(this.getClassLoader().getResourceAsStream("fileName.json")))

OR

new InputStreamReader(new ClassPathResource("fileName.json", this.getClassLoader()).getInputStream())

Don't use FileReader or File

use InputStreamReader, InputStream etc

Ziaullhaq Savanur
  • 1,848
  • 2
  • 17
  • 20
  • 1
    I used InputStreamReader as your answered here and it solved my issue. Thanks slot bro – JerVi Mar 01 '21 at 09:18