如何获取JavaJAR文件中的资源路径我正试图找到一条通往资源的道路,但我没有运气。这是可行的(在IDE和JAR中都是如此),但是这样我就无法获得文件的路径,只有文件内容:ClassLoader classLoader = getClass().getClassLoader();PrintInputStream(classLoader.getResourceAsStream("config/netclient.p"));如果我这么做:ClassLoader classLoader = getClass().getClassLoader();File file = new File(classLoader.getResource("config/netclient.p").getFile());结果是:java.io.FileNotFoundException: file:/path/to/jarfile/bot.jar!/config/netclient.p (No such file or directory)有办法获得资源文件的路径吗?
3 回答
![?](http://img1.sycdn.imooc.com/545868550001f60202200220-100-100.jpg)
慕后森
TA贡献1802条经验 获得超5个赞
![?](http://img1.sycdn.imooc.com/54584dc4000118d302200220-100-100.jpg)
MYYA
TA贡献1868条经验 获得超4个赞
String path = this.getClass().getClassLoader().getResource(<resourceFileName>).toExternalForm()
getResource
toExternalForm()
添加回答
举报
0/150
提交
取消