gpt4 book ai didi

java - 导出的 jar 文件缺少对资源的访问

转载 作者:行者123 更新时间:2023-12-01 11:57:19 25 4
gpt4 key购买 nike

如果我在 eclipse 中运行以下代码,我的程序编译没有问题。如果我尝试将程序导出为可运行的 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/test/

com/test/Tester.class

res/

res/myFile.txt

最佳答案

如果是jar则没有这样的文件。

改用Tester.class.getResourceAsStream("/myFile.txt")并使用流

更新:查看更多herehere

关于java - 导出的 jar 文件缺少对资源的访问,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28344810/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com