我想从我的jar中读取资源,如下所示:
File file;
file = new File(getClass().getResource("/file.txt").toURI());
BufferedReader reader = new BufferedReader(new FileReader(file));
//Read the file
当在Eclipse中运行它时,它工作得很好,但是如果我将它导出到一个jar中,然后运行它,就会有一个IllegalArgumentException:
Exception in thread "Thread-2"
java.lang.IllegalArgumentException: URI is not hierarchical
我真的不知道为什么,但通过一些测试,我发现如果我改变
file = new File(getClass().getResource("/file.txt").toURI());
至
file = new File(getClass().getResource("/folder/file.txt").toURI());
然后它正好相反(它可以在jar中工作,但不能在eclipse中工作)。
我使用的是Eclipse,我的文件所在的文件夹是一个class文件夹。
https://stackoverflow.com/questions/20389255
复制相似问题