0

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 package
  • java -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?

1
  • 1
    The problem here is that your app.event.json is 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 of loadSchema, but it should be able to handle InputStream. As long as you stick to InputStream you should be good. Commented Nov 3, 2019 at 13:48

3 Answers 3

1

You need to include your src/main/resources directory to the classpath in your pom.xml using its relative path:

 <build> <resources> <resource> <directory>src/main/resources</directory> </resource> </resources> </build> 

You can read more about it here.

Sign up to request clarification or add additional context in comments.

1 Comment

thanks for your answer. I tried it with the absolute path as well I receive the same error. file does not exist /home/XXX/Dokumente/project/XXX/file:/home/XXX/Dokumente/project/XXX/target/app.jar!/BOOT-INF/classes!/app.event.json
1

Can you please check inside jar if it contains the mentioned file in classes folder. Also you can try below code to load the file from classpath.

Thread.currentThread().getContextClassLoader().getResource(<file_name>) 

If possible then keep that file outside the jar in some folder location and set that location as classpath when executing the jar.

1 Comment

Thanks for your answer. If i run mvn spring-boot:run the file exists inside of the jar. If I just run mvn package and java -jar XXX.jar it doesnt exist: /home/XXX/Dokumente/project/XXX/target/classes/XXX.json if I unpack the jaar maually I see the file in BOOT-INF/classes/
0

As Ropert Scholte mentioned to fix it I used Inputstream to load instead of loading it as File. With the Code below I fixed it:

private Reader loadSchema(String schemaName) throws JsonSchemaMissingException { ClassLoader classLoader = JsonSchemaValidator.class.getClassLoader(); InputStream fileStream = classLoader.getResourceAsStream(schemaName); if (fileStream == null) { log.LogErrorWithTransactionId("", "file does not exist "); throw new JsonSchemaMissingException("file does not exist"); } Reader reader = new InputStreamReader(fileStream, StandardCharsets.UTF_8); return reader; } 

Note since I use the file for validating a JSON Schema I needed to convert the Inputstream to a reader object

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.