を呼び出しSystem.getProperty("user.dir");
て現在のディレクトリを取得し、結果への相対パスを連結します。
例えば
String path = System.getProperty("user.dir");
path += relativePath;
編集:
説明:
例: 実行するプログラムは、常に 2 つ後ろのフォルダーに配置され、次に Temp dir に配置されます。
したがって、相対パスは..\\..\Temp\prog.exe.
あるコンピュータの にプログラムがあるとしますC:\Users\user\Documents\Program
。これが作業ディレクトリです。
そう:
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));