首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >从jar中读取资源文件

从jar中读取资源文件
EN

Stack Overflow用户
提问于 2013-12-05 08:48:51
回答 12查看 395.4K关注 0票数 308

我想从我的jar中读取资源,如下所示:

代码语言:javascript
复制
File file;
file = new File(getClass().getResource("/file.txt").toURI());
BufferedReader reader = new BufferedReader(new FileReader(file));

//Read the file

当在Eclipse中运行它时,它工作得很好,但是如果我将它导出到一个jar中,然后运行它,就会有一个IllegalArgumentException:

代码语言:javascript
复制
Exception in thread "Thread-2"
java.lang.IllegalArgumentException: URI is not hierarchical

我真的不知道为什么,但通过一些测试,我发现如果我改变

代码语言:javascript
复制
file = new File(getClass().getResource("/file.txt").toURI());

代码语言:javascript
复制
file = new File(getClass().getResource("/folder/file.txt").toURI());

然后它正好相反(它可以在jar中工作,但不能在eclipse中工作)。

我使用的是Eclipse,我的文件所在的文件夹是一个class文件夹。

EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20389255

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档