简体   繁体   English

读取属性文件时 getResourceAsStream 返回 null

[英]getResourceAsStream returns null when reading properties file

I want to load application.properties file form maven resource folder but getResourceAsStream returns null .我想从 maven 资源文件夹加载 application.properties 文件,但getResourceAsStream返回null here is my code:这是我的代码:

static {
    Properties props = new Properties(); 
    InputStream in = Configuration.class.getResourceAsStream("application.properties");
    props.load(in);
}

but InputStream is null .InputStreamnull Configuration class is located at org.elasticsearch.utils package and application.properties is located at src/main/resources . Configuration类位于org.elasticsearch.utils包, application.properties位于src/main/resources What is wrong?怎么了?

You're trying to read a classpath resource relative to the Configuration class.您正在尝试读取相对于Configuration类的类路径资源。

  • If you want to do this, the resource must be on the same relative path, ie your property file must be in src/main/resources/org/elasticsearch/utils .如果你想这样做,资源必须在相同的相对路径上,即你的属性文件必须在src/main/resources/org/elasticsearch/utils
  • Or you can instead use absolute path: /application.properties .或者您可以改用绝对路径: /application.properties

From what I am seeing, your setup is just right.就我所见,您的设置恰到好处。 Having the maven resource folder and the java folder both under "main" is totally fine.将 maven 资源文件夹和 java 文件夹都放在“main”下是完全没问题的。 I had a problem with the filename of the properties file.我的属性文件的文件名有问题。 I still do not understand why, but as soon I changed the name from application.properties to whateverYouWant.properties it suddenly works.我仍然不明白为什么,但是当我将名称从application.properties更改为whateverYouWant.properties它突然起作用了。

And have you enabled filtering for maven resources in the pom.xml?您是否在 pom.xml 中启用了对 Maven 资源的过滤?

    <build>

    <!--Allows access to maven resources-->
    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <filtering>true</filtering>
        </resource>
    </resources>

Here is a guide from Alex Miller Retrieve version from maven pom.xml in code这是 Alex Miller 在代码中从 maven pom.xml 检索版本的指南

And here is the full code example for accessing the properties in java code (I know you posted already nearly all of it, but just in case if somebody stumbles upon this post)这是访问 java 代码中的属性的完整代码示例(我知道你已经发布了几乎所有内容,但以防万一有人偶然发现这篇文章)

    public String getVersionNumber() {
    String version = "";
    ClassLoader classLoader = getClass().getClassLoader();
    final Properties properties = new Properties();
    try {
        properties.load(classLoader.getResourceAsStream("main.properties"));
        version = properties.getProperty("application.version");
    } catch (IOException ioException) {
        log.log(Level.WARNING, "Version number could not be retrieved from maven resources.");
    } catch (NullPointerException nullPointerException) {
        log.log(Level.WARNING, "Cannot find .properties file to read version number");
    }
     return version;
}

This is what I have tried and working:这是我尝试过和工作的: 在此处输入图片说明

@Jiri's answer is also correct. @Jiri 的回答也是正确的。

PS: "/config.properties" also works in my example. PS: "/config.properties"也适用于我的示例。 But if the this was inside ElasticTest class, then you should have to use just "config.properties" .但是如果 this 在 ElasticTest 类中,那么你应该只使用"config.properties"

you need to either add a / at the beginning of the rsource location string or move it to match the package structure or use the classloader rather than the class.您需要在 rsource 位置字符串的开头添加 / 或移动它以匹配包结构或使用类加载器而不是类。

When looking for a resource via a class the assumption is that the resource directory structure mirrors your class structure.通过类查找资源时,假设资源目录结构反映了您的类结构。

so your code is looking for the resource under src/main/resources/org/elastic...所以你的代码正在寻找 src/main/resources/org/elastic 下的资源...

  1. If the file "application.properties" is under "src/main/resources", use full path (in the sense of class loader):如果文件“application.properties”在“src/main/resources”下,使用完整路径(在类加载器的意义上):

    getResourceAsStream("/application.properties") getResourceAsStream("/application.properties")

  2. If you move your file to the same package as your class, you can use relative path:如果将文件移动到与类相同的包,则可以使用相对路径:

    getResourceAsStream("application.properties") getResourceAsStream("application.properties")

  3. If you move your file to the same other packages, use full path:如果您将文件移动到相同的其他包,请使用完整路径:

    getResourceAsStream("/my/package/name/application.properties") getResourceAsStream("/my/package/name/application.properties")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM