Java getResourceAsStream JAR inside WAR

前端 未结 3 1513
粉色の甜心
粉色の甜心 2020-12-18 09:51

I have a Java webapp WAR file that depends on multiple jars in it\'s WEB-INF\\lib directory. One of these JARS needs to load some config files by doing class.getClassL

3条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-18 10:08

    Assuming you're not making the rookie mistake of putting QueryLoader in a different JAR, the only problem I can see is that you're using File.separator yet appear (from your use of \) to be using Windows. When using getResourceAsStream, the separator is always a forward slash (/) just as if you're using a URL.

    If I change that I get this:

    QueryLoader.class.getClassLoader().getResourceAsStream(
              "/com/companyname/queries/" + fileName)
    

    Of course, if QueryLoader is in the com.companyname.queries package (along with the queries themselves) then you should simply do this:

    QueryLoader.class.getResourceAsStream(fileName)
    

    Simple as that. (It's documented that Class.getResourceAsStream qualifies relative filenames with the name of the containing package.)

提交回复
热议问题