JAR 的 target/classes 文件夹中不存在文件,应从资源项目文件夹中读取

发布于 2025-01-19 03:04:01 字数 997 浏览 2 评论 0原文

我正在尝试阅读。从我部署的代码中的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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

泛泛之交 2025-01-26 03:04:01

由于您已经添加了 maven 标签,我假设您正在使用 maven。

由于 .yaml 位于资源文件夹内,因此您应该使用 getResourceAsStream()

/src/main/resources/config.yaml:

first: value1
second: value2

要读取文件及其内容:

import java.util.Properties;
import java.io.InputStream;
import java.io.IOException;

public class Example {

    InputStream inputStream = null;
    final Properties properties = new Properties();

    public Example() {
        try {
            inputStream = 
                this.getClass().getClassLoader().getResourceAsStream("config.yaml");
            properties.load(inputStream);
        } catch (IOException exception) {
            LOG("ERROR", "Config file does not exist ");
        } finally {
            if (inputStream != null){
                try {
                    inputStream.close();
                } catch (Exception e) {
                    LOG("ERROR", "Failed to close input stream");
                }
            }
        }
     }

    public printValues(){
        LOG("INFO", "First value is: " + properties.getProperty("first"));
    }
}

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:

first: value1
second: value2

To read the file and its content:

import java.util.Properties;
import java.io.InputStream;
import java.io.IOException;

public class Example {

    InputStream inputStream = null;
    final Properties properties = new Properties();

    public Example() {
        try {
            inputStream = 
                this.getClass().getClassLoader().getResourceAsStream("config.yaml");
            properties.load(inputStream);
        } catch (IOException exception) {
            LOG("ERROR", "Config file does not exist ");
        } finally {
            if (inputStream != null){
                try {
                    inputStream.close();
                } catch (Exception e) {
                    LOG("ERROR", "Failed to close input stream");
                }
            }
        }
     }

    public printValues(){
        LOG("INFO", "First value is: " + properties.getProperty("first"));
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文