Daredevil
Daredevil

Reputation: 1792

java - reading config file in same directory as jar file

I have a simple program in Intellij that I made just to test out reading file path of config file.

I created a simple test case where I would use a timer to print "Hello world" periodically in N intervals where N is in milliseconds and N is configurable.

This is the code:

public void schedule() throws Exception {

       Properties props=new Properties();

        String path ="./config.properties";
        FileInputStream fis=new FileInputStream(path);
        BufferedReader in1=new BufferedReader(new InputStreamReader(fis));
       // InputStream in = getClass().getResourceAsStream("/config.properties");

        props.load(in1);
        in1.close();
        int value=Integer.parseInt(props.getProperty("value"));






        Timer t=new Timer();
        t.scheduleAtFixedRate(
                new TimerTask() {
                    @Override
                    public void run() {
                      // System.out.println("HELEOELE");

                        try {
                           // test.index();
                            System.out.println("hello ");
                        } catch (Exception e) {
                            e.printStackTrace();
                        }

                    }
                },
              0,
                value);





    }

What I did was I set value as N in a config file where it can be changed by anyone without touching the actual code. So I compiled the jar file, and I placed both config.properties and jar file in same folder or directory. I want to be able to change make N changeable so I don't need to re-compile the jar again and again everytime.

Note: the config properties file is created manually and placed in same directory as the jar. And I am executing the jar in command prompt.

However, it seems when I try to run it, it doesn't recognize the file path.

"main" java.io.FileNotFoundException: .\config.properties (The system cannot find the file specified)

I've looked into many issues regarding reading config files outside of jar file and none of them worked for me. Am I doing any mistake here?

Upvotes: 2

Views: 2758

Answers (1)

Aaron
Aaron

Reputation: 24812

./config.properties is a relative path that points to a config.properties file in the current working directory.

The current working directory, unless changed by System.setProperty("user.dir", newPath), will be the directory from which you launched the JVM currently handling your code.

To get your jar to work as it currently is, you have two ways available :

  1. copy the config.properties file to the directory you are executing java from
  2. change the directory you are running java from to the one that contains the config.properties

You may also consider letting the user specify where to get the properties file from :

String path = System.getProperty("propertiesLocation", "config.properties");

You would then be able to specify a location for the property file when calling your jar :

java -jar /path/to/your.jar -DpropertiesLocation=/path/to/your.properties

Or call it as you did before to search for the properties at its default location of config.properties in the current working directory.

Upvotes: 3

Related Questions