Search code examples
javajakarta-eeglassfishpayara

How to reference a file in Payara


Simply put I need to read a schema.json file for org.everit.json.schema. In Eclipse I naively for testing I did it like this:

new String(Files.readAllBytes(Paths.get("./src/main/resources/schemas/Schema.json"))));

However when compiling and deploying to Payara I am getting a java exception file not found.

Where is or what is the best way to reference a file on the server in code (cannot seem to find a documented approach for this).


Solution

  • The path where Schema.json is located indicates a Maven project hierarchy. As such, the src/main/resources folder is in the classpath. The best way to get the contents of this file from a non-static method is:

    InputStream is = getClass().getResourceAsStream("/schemas/Schema.json");
    

    And read the InputStream.

    2 notes:

    • Exception and resource handling not shown
    • From a static method you need to use ClassLoader.getResourceAsStream()