in my resources folder:
src/main/resources I have two files the application.properties file and a JSON file app.schema.json
I have the following function:
private File loadSchema(String schemaName) throws JsonSchemaMissingException { ClassLoader classLoader = JsonSchemaValidator.class.getClassLoader(); File file = new File(Objects.requireNonNull(classLoader.getResource("app.schema.json")).getFile()); if (!file.exists()) { log.LogErrorWithTransactionId("", "file does not exist " + file.getAbsolutePath()); throw new JsonSchemaMissingException("file does not exist"); } return file; } If I run mvn spring-boot:run it successfully find the file. If I run:
mvn packagejava -jar app.jar
I get a NullPointer because the following file does not exist:
/home/XXX/Docs/project/XXX/file:/home/ghovat/Docs/project/XXX/target/app.jar!/BOOT-INF/classes!/app.event.json In my pom.xml in build I added with and without the following setting:
<resources> <resource> <directory>src/main/resources</directory> </resource> </resources> Neither way it works. It can reach the file if I run mvn spring-boot:run but also if I run mvn clean package spring-boot:repackage and java -jar target/app.jar It doesnt find the file.
I checked all the docs and tried several different ways to load the file but either way it doesnt find it.
How can I make the file available?
app.event.jsonis not accessible as a file, as it is inside the app.jar file. You must access it as a resource. Your code doesn't show wht happens with the result ofloadSchema, but it should be able to handleInputStream. As long as you stick to InputStream you should be good.