ghovat
ghovat

Reputation: 1043

Spring / Maven load file from classpath

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:

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?

Upvotes: 0

Views: 2403

Answers (3)

ghovat
ghovat

Reputation: 1043

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

Upvotes: 0

Ashok Prajapati
Ashok Prajapati

Reputation: 374

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.

Upvotes: 1

VadymVL
VadymVL

Reputation: 5566

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.

Upvotes: 1

Related Questions