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

时间:2023-02-06 13:17:02

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/classes folder generated after maven compiles the project.

我从Git存储库导入了一个项目,并在Eclipse中向其添加了Maven特性。在resources文件夹中,我添加了一个名为mycon .properties的配置文件。现在,每当我尝试从我的Java代码中打开这个文件时,就会得到FileNotFoundException。该文件还存在于maven编译项目后生成的目标/类文件夹中。

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 props is a Properties object.

道具是属性对象。

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

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

6 个解决方案

#1


89  

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.

如果编译后文件被放置在目标/类之下,那么它已经位于构建路径的一部分目录中。目录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 ...

#2


13  

I think you need to put it under src/main/resources and 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/classes and you want to load it from there do the following:

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

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

#3


3  

If it is simple application then getSystemResourceAsStream can also be used.

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

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

#4


2  

Right click the Resources folder and select Build Path > Add to Build Path

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

#5


2  

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);
        }

#6


1  

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

我相信,如果您使用的是“myconf”,那么应该是从Eclipse运行。属性作为相对路径,文件结构应该是这样的

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

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

#1


89  

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.

如果编译后文件被放置在目标/类之下,那么它已经位于构建路径的一部分目录中。目录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 ...

#2


13  

I think you need to put it under src/main/resources and 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/classes and you want to load it from there do the following:

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

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

#3


3  

If it is simple application then getSystemResourceAsStream can also be used.

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

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

#4


2  

Right click the Resources folder and select Build Path > Add to Build Path

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

#5


2  

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);
        }

#6


1  

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

我相信,如果您使用的是“myconf”,那么应该是从Eclipse运行。属性作为相对路径,文件结构应该是这样的

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

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