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

Viewed 19460

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

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

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.

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

Related