Java 无法从资源目录加载属性文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20348657/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 01:14:42  来源:igfitidea点击:

Cannot load properties file from resources directory

javaeclipsemavenmaven-3

提问by artaxerxe

I imported a project from a Git repository and added Maven nature to it in Eclipse. In the resources folder, I added a configuration file called myconf.properties. Now, whenever I try to open this file from my Java code, I get FileNotFoundException. The file is also present in the target/classesfolder generated after maven compiles the project.

我从 Git 存储库导入了一个项目,并在 Eclipse 中向它添加了 Maven 性质。在资源文件夹中,我添加了一个名为myconf.properties. 现在,每当我尝试从我的 Java 代码打开这个文件时,我都会得到FileNotFoundException. 该文件也存在于target/classesmaven 编译项目后生成的文件夹中。

Can anyone tell me what can be the problem? My Java code that tries to load this file is:

谁能告诉我可能是什么问题?我尝试加载此文件的 Java 代码是:

props.load(new FileInputStream("myconf.properties"));

where propsis a Propertiesobject.

哪里props是一个Properties对象。

Can anyone give me some hints on how to solve this issue?

谁能给我一些关于如何解决这个问题的提示?

采纳答案by Seelenvirtuose

If the file is placed under target/classes after compiling, then it is already in a directory that is part of the build path. The directory src/main/resources is the Maven default directory for such resources, and it is automatically placed to the build path by the Eclipse Maven plugin (M2E). So, there is no need to move your properties file.

如果文件在编译后放在 target/classes 下,那么它已经在作为构建路径一部分的目录中。目录 src/main/resources 是此类资源的 Maven 默认目录,它会被 Eclipse Maven 插件 (M2E) 自动放置到构建路径中。因此,无需移动您的属性文件。

The other topic is, how to retrieve such resources. Resources in the build path are automatically in the class path of the running Java program. Considering this, you should always load such resources with a class loader. Example code:

另一个主题是,如何检索此类资源。构建路径中的资源自动位于运行的 Java 程序的类路径中。考虑到这一点,您应该始终使用类加载器加载此类资源。示例代码:

String resourceName = "myconf.properties"; // could also be a constant
ClassLoader loader = Thread.currentThread().getContextClassLoader();
Properties props = new Properties();
try(InputStream resourceStream = loader.getResourceAsStream(resourceName)) {
    props.load(resourceStream);
}
// use props here ...

回答by Paul Samsotha

I believe running from Eclipse, if you're using "myconf.properties"as the relative path, You file structure should look somehting like this

我相信从 Eclipse 运行,如果你使用“myconf.properties”作为相对路径,你的文件结构应该看起来像这样

ProjectRoot
         src
         bin
         myconf.properties

Eclipse will look for the the file in the project root dir if no other dirs are specified in the file path

如果文件路径中没有指定其他目录,Eclipse 将在项目根目录中查找该文件

回答by Kevin Bowersox

Right click the Resourcesfolder and select Build Path > Add to Build Path

右键单击Resources文件夹并选择Build Path > Add to Build Path

回答by dev2d

I think you need to put it under src/main/resourcesand load it as follows:

我认为你需要把它放在下面src/main/resources并按如下方式加载它:

props.load(new FileInputStream("src/main/resources/myconf.properties"));

The way you are trying to load it will first check in base folder of your project. If it is in target/classesand you want to load it from there do the following:

您尝试加载它的方式将首先检查项目的基本文件夹。如果它在里面target/classes并且您想从那里加载它,请执行以下操作:

props.load(new FileInputStream("target/classes/myconf.properties"));

回答by Karthik Prasad

I use something like this to load properties file.

我使用这样的东西来加载属性文件。

        final ResourceBundle bundle = ResourceBundle
                .getBundle("properties/errormessages");

        for (final Enumeration<String> keys = bundle.getKeys(); keys
                .hasMoreElements();) {
            final String key = keys.nextElement();
            final String value = bundle.getString(key);
            prop.put(key, value);
        }

回答by rns

If it is simple application then getSystemResourceAsStreamcan also be used.

如果是简单的应用程序,那么也可以使用getSystemResourceAsStream

try (InputStream inputStream = ClassLoader.getSystemResourceAsStream("config.properties"))..

回答by Deep Nirmal

Using ClassLoader.getSystemClassLoader()

使用 ClassLoader.getSystemClassLoader()

Sample code :

示例代码:

Properties prop = new Properties();
InputStream input = null;
try {
    input = ClassLoader.getSystemClassLoader().getResourceAsStream("conf.properties");
    prop.load(input);

} catch (IOException io) {
    io.printStackTrace();
}