1

I want to load application.properties file form maven resource folder but getResourceAsStream returns null. here is my code:

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

but InputStream is null. Configuration class is located at org.elasticsearch.utils package and application.properties is located at src/main/resources. What is wrong?

Nariman Esmaiely Fard
  • 505
  • 1
  • 10
  • 24

5 Answers5

2

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. 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.

And have you enabled filtering for maven resources in the pom.xml?

    <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

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)

    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;
}
Wumba
  • 99
  • 1
  • 12
  • I had the same problem with `application.properties'. Now renamed it to `app.properties` and it works. – horvoje Aug 16 '23 at 00:42
1

You're trying to read a classpath resource relative to the Configuration class.

  • If you want to do this, the resource must be on the same relative path, i.e. your property file must be in src/main/resources/org/elasticsearch/utils.
  • Or you can instead use absolute path: /application.properties.
Jiri Tousek
  • 12,211
  • 5
  • 29
  • 43
  • I tried both ways as you said but still getting null. – Nariman Esmaiely Fard Jun 02 '18 at 07:57
  • Jeri, you dont understand how IDEs work and how they map directories to classpath. – mentallurg Jun 02 '18 at 07:58
  • @mentallurg the use of `src/main/resources` standard directory structure strongly hints at using Maven. If that's the case, the used IDE shouldn't matter. – Jiri Tousek Jun 02 '18 at 08:03
  • @JiriTousek after I moved resource file, should I use Configuration.class.getResourceAsStream("application.properties") or Configuration.class.getResourceAsStream("org/elasticsearch/utils/application.properties") ? – Nariman Esmaiely Fard Jun 02 '18 at 08:04
  • @NarimanEsmaielyFard when you open the compiled `jar` (open, not launch - it's a ZIP archive), what path the property file is on, within the jar? – Jiri Tousek Jun 02 '18 at 08:09
  • @NarimanEsmaielyFard if you moved it to `src/main/resources/org/elasticsearch/utils`, you keep the `application.properties` path. – Jiri Tousek Jun 02 '18 at 08:10
  • @JiriTousek using Configuration.class.getResourceAsStream("org/elasticsearch/utils/application.properties") worked. thanks. – Nariman Esmaiely Fard Jun 02 '18 at 08:15
0

This is what I have tried and working: enter image description here

@Jiri's answer is also correct.

PS: "/config.properties" also works in my example. But if the this was inside ElasticTest class, then you should have to use just "config.properties".

Community
  • 1
  • 1
Abhijith S
  • 526
  • 5
  • 17
-1

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.

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...

Meir Maor
  • 1,200
  • 8
  • 18
-1
  1. If the file "application.properties" is under "src/main/resources", use full path (in the sense of class loader):

    getResourceAsStream("/application.properties")

  2. If you move your file to the same package as your class, you can use relative path:

    getResourceAsStream("application.properties")

  3. If you move your file to the same other packages, use full path:

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

mentallurg
  • 4,967
  • 5
  • 28
  • 36