WildFly - getting resource from WAR

后端 未结 7 708
既然无缘
既然无缘 2020-12-21 00:13

I am using the following method to get a resource from WAR file in WildFly:

this.getClass().getResource(relativePath)

It works when the app

7条回答
  •  粉色の甜心
    2020-12-21 00:50

    I was recently trying to figure out how to access a file within my own war in Java. The following is how the java classes and resources are packaged in the war file:

    WAR
     `-- WEB-INF
            `-- classes (where all the java classes are)
            `-- resourcefiles
                       `-- resourceFile1
    

    My target file was resourceFile1. To get that file, I just did the following in code:

    InputStream inStream = this.class.getClassLoader().getResourceAsStream("resourcefiles/resourceFile1");
    

    In this case the resource files would need to be in the same folder as the classes folder containing the java classes. Hopefully others find this helpful.

提交回复
热议问题