Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Boot uber JAR: cannot be resolved to absolute file path because it does not reside in the file system

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

like image 856
Salah Atwa Avatar asked Dec 18 '22 20:12

Salah Atwa


2 Answers

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();
}
like image 85
Do Nhu Vy Avatar answered Jan 15 '23 02:01

Do Nhu Vy


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.

like image 38
nordenvall Avatar answered Jan 15 '23 01:01

nordenvall