Java getResourceAsStream JAR inside WAR

前端 未结 3 1505
粉色の甜心
粉色の甜心 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:23

    Managed to get it to work by using Spring's resource loader instead

    public String loadQuery(String fileName) {
      final String newline = "\n";
    
      ApplicationContext ctx = new ClassPathXmlApplicationContext();
      Resource res = ctx.getResource("classpath:/com/msi/queries/" + fileName);
      BufferedReader reader;
      StringBuilder sb = new StringBuilder();
      try {
         reader = new BufferedReader(new InputStreamReader(res.getInputStream()));
         String line;
    
         while ((line = reader.readLine()) != null) {
            sb.append(line);
            sb.append(newline);
         }
      } catch (IOException e) {
         LOGGER.error(e);
      }
    
      return sb.toString();
    

    }

提交回复
热议问题