JAR 的 target/classes 文件夹中不存在文件,应从资源项目文件夹中读取
我正在尝试阅读。从我部署的代码中的Java项目中的Resources文件夹中的配置。我能够从本地笔记本电脑中阅读,但是部署为JAR .Manifest文件后,它说路径不存在。
因此,我的Java Maven Project Str:src/main/java/..和配置路径如下:
Java代码读取此config Where file.exists()
始终返回falses 。
试验1:config路径是:src/main/resources/config.yaml
。
File configPath = new File(Objects.requireNonNull(getClass().getClassLoader().getResource("config.yaml")).getFile());
if (!configPath.exists()) {
Log("ERROR", "Config file does not exist "); // this is printed
}
试验2:当配置路径为src/main/resources/feed/feed/configs/config.yaml
时。
File dir = new File(Objects.requireNonNull(getClass().getClassLoader().getResource("feed/configs")).getFile());
if (!dir.exists()) {
Log("ERROR", "Config folder does not exist, "ERROR"); // THIS IS PRINTED
return;
}
File[] configFiles = configPath.listFiles(); // NOT EXECUTED AS ABOVE IS RETURNED
I am trying to read. a config from my resources folder in Java project from my deployed code. I am able to read from my local laptop but after deployment as JAR .manifest file, it says path does not exist.
So my Java maven project str: src/main/java/..
and config path as follows:
Java code to read this config where file.exists()
always returns false.
Trial 1: When config path is : src/main/resources/config.yaml
.
File configPath = new File(Objects.requireNonNull(getClass().getClassLoader().getResource("config.yaml")).getFile());
if (!configPath.exists()) {
Log("ERROR", "Config file does not exist "); // this is printed
}
Trial 2: When config path is src/main/resources/feed/configs/config.yaml
.
File dir = new File(Objects.requireNonNull(getClass().getClassLoader().getResource("feed/configs")).getFile());
if (!dir.exists()) {
Log("ERROR", "Config folder does not exist, "ERROR"); // THIS IS PRINTED
return;
}
File[] configFiles = configPath.listFiles(); // NOT EXECUTED AS ABOVE IS RETURNED
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于您已经添加了 maven 标签,我假设您正在使用 maven。
由于 .yaml 位于资源文件夹内,因此您应该使用
getResourceAsStream()
/src/main/resources/config.yaml:
要读取文件及其内容:
Since you have added the maven tag, I am assuming you are using maven.
As the .yaml is inside the resources folder you should be using
getResourceAsStream()
/src/main/resources/config.yaml:
To read the file and its content: