Search code examples
javarelative-path

Relative file path in java


I have this code:

 Process p = Runtime.getRuntime().exec(command.toString(), null, 
                                          new File("D:\\Cognity"));

But the thing is the Cognity directory is not always in D:\, it could be in C:\ etc. So my question is if there is a way to give it a relative path so I don't have to change this code depending on the PC that uses the program?


Solution

  • As System.getProperty("user.dir")); returns the directory from which the JVM was launched you still can't guarantee whether you're in C:\ or D:\ My advice would be to pass the Cognity location in as a -D commandline argument and use it like this:

    Process p = Runtime.getRuntime().exec(command.toString(), null, new File(System.getProperty("cognity.dir")));