Reputation: 1910
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?
Upvotes: 3
Views: 9759
Reputation: 3934
Call System.getProperty("user.dir");
to get your current dir, and concat the relative path to the result.
e.g.
String path = System.getProperty("user.dir");
path += relativePath;
EDIT:
Clarification:
For example: The program you want to run is always located two folders backwards, and then at Temp dir.
So the relative path is ..\\..\Temp\prog.exe.
Let's say that in one computer you have your program located at C:\Users\user\Documents\Program
, so this is the working directory.
So:
String path = System.getProperty("user.dir"); //returns the working directory
String relativePath = "\\ ..\\..\\Temp"; //no matter what is the first path, this is the relative location to the current dir
path += relativePath;
//in this example, path now equals C:\Users\user\Documents\Program\..\..\Temp\prog.exe = C:\Users\user\Temp\prog.exe
Process p = Runtime.getRuntime().exec(command.toString(), null, new File(path));
Upvotes: 2
Reputation: 96
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")));
Upvotes: 2
Reputation: 851
You could use a config-File to set the absolute-path or you create that directory in a relative path to the jar-file.
Upvotes: 0