如果我在eclipse中运行下面的代码,我的程序编译时没有问题。如果我尝试将程序导出为可运行的jar文件,则无法找到我的资源。访问导出的jar文件时丢失的资源
public class Test {
public static void main(String[] args) {
File file = new File(Tester.class.getClassLoader().getResource("res/myFile.txt").getFile());
if(!file.exists()) { System.out.println("File not found."); }
}
}
文件夹结构(通过运行jar tf test.jar
):
COM/
COM /测试/
COM /测试/ Tester.class
RES/
res/myFile.txt
请告诉我们您的文件夹structire。该文件包含在你的jar吗? – Jens 2015-02-05 13:11:56
@Jens是该文件包含在jar中。我打开它来仔细检查。 – 2015-02-05 13:12:42
,它在'res'文件夹下? – Jens 2015-02-05 13:13:27