read file from jar in spring-boot
Asked Answered
P

0

7

I am using Spring boot 2.0.4, trying read a json file which is placed in resources/ directory, but getting the 'FileNotFoundException'.

I have referred a lot of pages from Stackoverflow, and other links, but no success so far.

Following is the stack trace snippet, I am not sure why there is '!' in the classpath:

/opt/springboot/myProject.jar!/BOOT-INF/classes!/schema/jsonValidationSchema.json at o.s.u.ResourceUtils.getFile(ResourceUtils.java:217) at o.s.u.ResourceUtils.getFile(ResourceUtils.java:180)

This is the code snippet:

        InputStream in = new ClassPathResource("jsonValidationSchema.json").getInputStream();
        JsonNode schemaJsonNode = JsonLoader.fromReader(new InputStreamReader(in, "UTF-8"));
        JsonSchema schemaNode = ValidationUtils.getSchemaNode(schemaJsonNode);

References:

File inside jar is not visible for spring

Read file from resources folder in Spring Boot

https://smarterco.de/java-load-file-from-classpath-in-spring-boot/

java.io.FileNotFoundException: class path resource cannot be opened because it does not exist

java.io.FileNotFoundException: class path resource

Penetrance answered 8/5, 2019 at 13:54 Comment(2)
here it is you can use this. I had same error too. -> https://mcmap.net/q/81459/-read-file-from-resources-folder-in-spring-bootJurassic
The exclamation point is to indicate that the location is inside the jar and not part of the file system.Rael

© 2022 - 2024 — McMap. All rights reserved.