简体   繁体   中英

Unable to read properties file in java

I have main Class which is TestDriver.java , In which I am calling functions such as read to excel files and all and for that, I need to give an excel file path in java so instead of hardcoding I am using Properties file and using that I am Calling my function. For reading properties file I have given file path but I am unable to read properties file...

File: TestDriver.java

public class TestDriver 
{
      public static void main(String args[]){
            String filePath = "E:\\Project\\HRMSAutomation\\";

            Globals gbl = new Globals(filePath);

            // Functions.....

      }
}

File : Globals.java

public class Globals 
{
    static String filePath;

    public Globals(String path) {
        this.filePath = path;
    }

    static Properties prop = Genlib.readConfig(filePath+"config.properties");
    //readConfig is a method which will read the properties file
    public static final String LOGIN_URL = prop.getProperty("loginUrl");   
}

File : Config.properties

loginUrl = someurl....
testsRoot = tests
configFileDir = E:/Project/Automation/Data/ConfigData/

Error Log.

java.io.FileNotFoundException: nullconfig.properties (The system cannot find the file specified)
    at java.io.FileInputStream.open0(Native Method)
    at java.io.FileInputStream.open(Unknown Source)
    at java.io.FileInputStream.<init>(Unknown Source)
    at gtlib.Genlib.readConfig(Genlib.java:96)
    at projlib.Globals.<clinit>(Globals.java:15)
    at testDrivers.TestDriver.main(TestDriver.java:89)

I am getting Error because filePath variable getting null value may be some mistake in Globals.java file

Im Java, static fields' initialization runs before the constructor. So, when it initializes, filePath is still null . Place the initialization in the constructor like this:

public class Globals {
    static String filePath;
    public Globals(String path) {
        this.filePath = path;
        prop = Genlib.readConfig(filePath+"config.properties"); //readConfig is a method which will read the properties file
    }
    static Properties prop;
    public static final String LOGIN_URL = prop.getProperty("loginUrl");
}

Also, if this is a static class, I would recommend you to use a static methis to initialize it and not a constructor.

Guess you are using plain java coding style.

by following code you can load property and retrieve certain parameter

Properties prop = new Properties(); 
Class clazz = Object.class; 
InputStream stream = clazz.getResourceAsStream({path}/Config.properties"); prop.load(stream); 
filePath = prop.getProperty("configFileDir");

In case you use SpringBoot, work become much easier

Is the config.properties file that exact name or Config.properties ?

in Globals.java you refer to: filePath+"config.properties"

I am not sure about Genlib but below is the simple way of loading property file. The path which you have to supply is relative path of the file. For example the file could be at

src/test/resources

so you just have to do it like below:

FileInputStream configFile = new FileInputStream("src/test/resources" + File.separator +  "config.properties");
Properties configProperties = new Properties();
configProperties.load(configFile);

Your file path is set in the constructor of the class, but your property object is using a static class variable, that is initialized when that variable is created.

So this means that when your property object is initialized, the file path is not set yet, so the properties is trying to open the filename of "nullconfig.properties".

Why are you storing those as static? If they are not necessary to be statics, you could move them all into class variables, and have the constructor open the property file instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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