Category Archives: Cucumber

How to load properties file in Java

We can load properties file in Java as shown in below example. Note that Environment.properties file is kept in test resources directory.

import java.io.InputStream;
import java.util.Properties;

public class PropertyTest {
    public static void main(String[] args) {

        String resourceName = "Environment.properties";
   
        Properties props = new Properties();
        try{

            InputStream resourceStream = null;

            resourceStream = ClassLoader.getSystemResourceAsStream(resourceName);

           //resourceStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(resourceName);

            props.load(resourceStream);
            System.out.println("webserver.url -> " + props.getProperty("webserver.url"));
            props.list(System.out);
        }catch (Exception ex){
            System.out.println("Exception " + ex.toString());
        }
    }
}

Here is the output of above code.

webserver.url -> http://www.softpost.org
— listing properties —
webserver.admin=watson
webserver.url=http://www.softpost.org