How to specify the path to resources inside a war file?

I created my project using maven and spring using text editors. I can run the root folder project using the command on the terminal

mvn spring-boot:run

I reference my source files inside a java file using the java operator

Document doc = builder.parse("src/main/resources/data/resorts.xml");

Everything is working fine.

When I export the entire project as a war file using the command

mvn package

at the terminal

I get a war file in the target folder of the root directory

I launched the war file using the command

java -jar filename.war

There is no compilation error, but at runtime it shows a java.io.FileNotFoundException error

I think that I did not specify the path to the link file correctly

Can you mention how the relative path should be mentioned in the path bar so that it can be run from a war file.

My directory structure

.
    |-- src
    |   `-- main
    |       |-- java
    |       |   `-- hello
    |       |       `-- org
    |       |           `-- json
    |       `-- resources
    |           |-- data
    |           `-- templates
    `-- target
        |-- classes
        |   |-- data
        |   |-- hello
        |   |-- org
        |   |   `-- json
        |   `-- templates
        |-- generated-sources
        |   `-- annotations
        |-- gs-handling-form-submission-0.1.0
        |   |-- META-INF
        |   `-- WEB-INF
        |       |-- classes
        |       |   |-- data
        |       |   |-- hello
        |       |   |-- org
        |       |   |   `-- json
        |       |   `-- templates
        |       `-- lib
        |-- maven-archiver
        `-- maven-status
            `-- maven-compiler-plugin
                `-- compile
                    `-- default-compile

    33 directories

java hello.

+4
1

, , URI, , .

Application.class.getClassLoader().getResource("path-to/filename.txt").getPath()

: /path-to-your/application.jar!/path-to/filename.txt

File Stream , FileNotFoundException

, InputStream. .

:

InputStream is = Application.class.getClassLoader()
            .getResourceAsStream("data/resorts.xml");

is.

0

Source: https://habr.com/ru/post/1618537/


All Articles