Java 正确加载属性文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26358139/
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-11-02 09:47:56  来源:igfitidea点击:

Java load properties file correctly

javaclassloaderproperties-file

提问by machinery

I have the following project structure:

我有以下项目结构:

ProjectName/src/java/com/main/Main.java

And I have a properties file in the following folder:

我在以下文件夹中有一个属性文件:

ProjectName/config/settings.properties

Now I tried to load the properties file in the Main.java with:

现在我尝试在 Main.java 中加载属性文件:

InputStream input = Main.class.getResourceAsStream("/config/settings.properties");
Properties prop = System.getProperties();
prop.load(input);

But this does not work. How is it the right way to do it?

但这不起作用。怎么做才是正确的呢?

Edit: I got the following error:

编辑:我收到以下错误:

Exception in thread "main" java.lang.NullPointerException
    at java.util.Properties$LineReader.readLine(Unknown Source)
    at java.util.Properties.load0(Unknown Source)
    at java.util.Properties.load(Unknown Source)

Edit2: Now it works with eclipse. I did the following (adapted from getResourceAsStream() is returning null. Properties file is not loading)

Edit2:现在它适用于eclipse。我做了以下(改编自getResourceAsStream() 返回 null。属性文件未加载

1) This directory [config] must be put into the "build path". Right-click the directory in the Package Explorer or Project Explorer view, select "Build Path", then "Use as Source Folder". Note: This build path will be the class path for the project, when you run it.

1)这个目录[config]必须放在“构建路径”中。右键单击 Package Explorer 或 Project Explorer 视图中的目录,选择“Build Path”,然后选择“Use as Source Folder”。注意:当您运行它时,此构建路径将是项目的类路径。

2) As the config directory now is part of your class path and contains your properties file, you can simply load it with InputStream input = Server.class.getClassLoader().getResourceAsStream("settings.properties");

2)由于配置目录现在是您的类路径的一部分并包含您的属性文件,您可以简单地加载它 InputStream input = Server.class.getClassLoader().getResourceAsStream("settings.properties");

This works well for eclipse but not yet for a jar. I think I also have to add the build path somehow to the ant script.

这对 eclipse 很有效,但对 jar 还不行。我想我还必须以某种方式将构建路径添加到 ant 脚本中。

How can I do that?

我怎样才能做到这一点?

Edit3: If I take the settings.properties out of the config folder in the jar, then it works. Why? I want it in the config folder in the jar too.

Edit3:如果我从 jar 中的 config 文件夹中取出 settings.properties,那么它就可以工作。为什么?我也希望它在 jar 的 config 文件夹中。

回答by afzalex

Use following :

使用以下:

InputStream input = Main.class.getResourceAsStream("../../../../config/settings.properties");
Properties prop = System.getProperties();
prop.load(input);

By adding ../I am getting 1 step backward in your filesystem. In your code you were using /config....., which means C:/config.....(if it is in C drive).

通过添加,../我在您的文件系统中倒退了 1 步。在您使用的代码中/config.....,这意味着C:/config.....(如果它在 C 驱动器中)

The following will also work in your case. (but not in zipped file)

以下内容也适用于您的情况。(但不在压缩文件中)

InputStream input = new FileInputStream("config/settings.properties");
Properties prop = System.getProperties();
prop.load(input);

回答by Roshith

Three things

三件事

  1. You need add the settings.properties to a jar (can be the same jar as your Main)
  2. Your classpath should include this jar
  3. You need to use the context Class Loader to load the properties file in you Main.
  1. 您需要将 settings.properties 添加到 jar(可以与 Main 相同的 jar)
  2. 你的类路径应该包括这个 jar
  3. 您需要使用上下文类加载器来加载 Main 中的属性文件。

Classloader cl = Thread.currentThread().getContextClassLoader();

类加载器 cl = Thread.currentThread().getContextClassLoader();

cl.getResourceAsStream("settings.properties");

cl.getResourceAsStream("settings.properties");

回答by WidWing

getResourceAsStreamfind files relatively compiled (class) files on in jar file. Example: Your compiled files are in bindirectory than you should put properties file to bin/config/settings.propertiesfor give access to it.

getResourceAsStreamclass在 jar 文件中查找相对编译 ( ) 文件的文件。示例:您编译的文件位于bin目录中,而不是您应该将属性文件放入以bin/config/settings.properties访问它的目录中。

回答by JavaLearner

You are using maven. So follow the maven structure and keep your resources like prop, xml etc files in resourcefolder and then call.

您正在使用 Maven。因此,请遵循 maven 结构并将您的资源(如 prop、xml 等文件)保存在resource文件夹中,然后调用。

So folder structure would be like this

所以文件夹结构会是这样的

src/main/resource|
                 |
                  config|
                        |
                        settings.properties

InputStream input = Main.class.getClass().getResourceAsStream("/config/settings.properties");