'Reading .properties file in java

I have created a properties file in the location conf/Config.properties. The folder is under the project's root folder in Eclipse. I also added this to the .classpath.

I am reading the data from this file using the code:

InputStream in = getClass().getResourceAsStream("conf/Config.properties");
Properties properties = new Properties();
properties.load(in);
String fromEmail = properties.getProperty("emailID");
System.out.println("from email is " + fromEmail);
String fromEmailPass = properties.getProperty("emailPass");
String host = properties.getProperty("host");

This gives the error :

java.lang.NullPointerException
    at java.util.Properties$LineReader.readLine(Properties.java:418)
    at java.util.Properties.load0(Properties.java:337)
    at java.util.Properties.load(Properties.java:325)
    at com.sendum.integration.activities.notifications.ActivityImplSendActivationEmail.activateEmail(ActivityImplSendActivationEmail.java:23)

How do I read the data from the .properties file ?



Solution 1:[1]

It seems that getClass().getResourceAsStream("conf/Config.properties"); is returning null. this means that currently it isn't finding the file.

Try using, getReasourceAsStream("./conf/Config.properties"). This is relative path because it starts in the current directory, then finds conf/Config.properties or try using the absolute path, getReasourceAsStream("/Users/user/filepath/conf/Config.properties")

See here for a similar post.

Solution 2:[2]

Try with

getClass().getClassLoader().getResourceAsStream("conf/Config.properties");

Solution 3:[3]

In your stream path it tries to load the resource from relative path to your class location.

Change your InputStream as

InputStream in = getClass().getResourceAsStream("../conf/Config.properties");

Or otherwise give the full path of your file location in your InputStream

Solution 4:[4]

Try the below code for fetching the data from the properties file.

Properties prop = new Properties();
InputStream input = null;

input = this.getClass().getClassLoader().getResourceAsStream("conf/Config.properties");

// load a properties file
prop.load(input);

String str = prop.getProperty("key");//like userName

Solution 5:[5]

You can do the following as well.

public static void loadPropertiesToMemory() {
    Properties prop = new Properties();
    InputStream input = null;
    String filePathPropFile = "configuration.properties";
    try {
        input = App.class.getClassLoader().getResourceAsStream(filePathPropFile);
        prop.load(input);
    } catch (IOException ex) {
        ex.printStackTrace();
    } 
}

App is the class which consists the method of loading property file. This way uses getResourceAsStream() method of java.lang.ClassLoader class. This method takes the path of the file (or resource) to be loaded. This method considers all paths as absolute. That is, if you provide only the name of file, it will search the file inside project folders such as conf, src.

Please note that the path given to this method should NOT begin with ‘/’ as the path is considered as implicitly absolute.

Solution 6:[6]

package com.core;
 
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
 
public class MyPropWithinClasspath {
 
    private Properties prop = null;
     
    public MyPropWithinClasspath(){
         
        InputStream is = null;
        try {
            this.prop = new Properties();
            is = this.getClass().getResourceAsStream("info.properties");
            prop.load(is);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
     
    public String getPropertyValue(String key){
        return this.prop.getProperty(key);
    }
     
    public static void main(String a[]){
         
        MyPropWithinClasspath mpc = new MyPropWithinClasspath();
        System.out.println("db.host: "+mpc.getPropertyValue("account.no"));
        System.out.println("db.user: "+mpc.getPropertyValue("pin.no"));
        System.out.println("db.password: "+mpc.getPropertyValue("phone.no"));
    }
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Community
Solution 2 adarshr
Solution 3
Solution 4 Tanmay kumar shaw
Solution 5 Du-Lacoste
Solution 6