classLoader.getResource doesn't work in jar file

前端 未结 2 1836
眼角桃花
眼角桃花 2021-02-05 19:12
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
URL url = classLoader.getResource(\"com/x/y/z.cfg\");
File file = new File(url.getPath());
         


        
2条回答
  •  盖世英雄少女心
    2021-02-05 20:08

    When file is bundled inside the jar then it become byte stream instead of a normal File object.

    Try

    InputStream stram=getClass().getClassLoader().getResourceAsStream(relativePath);
    

    More Tutorial...

    Read similar post here and here

提交回复
热议问题