Salah Atwa
Salah Atwa

Reputation: 1728

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

Upvotes: 6

Views: 26890

Answers (3)

Ziaullhaq Savanur
Ziaullhaq Savanur

Reputation: 2348

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

Upvotes: 5

nordenvall
nordenvall

Reputation: 188

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.

Upvotes: 4

Vy Do
Vy Do

Reputation: 52526

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();
}

Upvotes: 9

Related Questions