我想从我的jar中读取一个资源,如下所示:
File file;
file = new File(getClass().getResource("/file.txt").到URI());
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").到URI());
到
file = new File(getClass().getResource("/folder/file.txt").到URI());
然后它的工作原理正好相反(它在jar中工作,但在eclipse中不工作).
我正在使用Eclipse,文件所在的文件夹位于类文件夹中.