3

私はこのコードを持っています:

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

しかし、問題は、Cognity ディレクトリが常に D:\ にあるとは限らず、C:\ などにある可能性があることです。したがって、私の質問は、相対パスを指定する方法があるかどうかです。したがって、このコードを変更する必要はありません。プログラムを使用する PC で?

4

3 に答える 3

2

JVM が起動されたディレクトリを返すようSystem.getProperty("user.dir"));に、C:\ または D:\ のどちらにいるかはまだ保証できません。私のアドバイスは、Cognityの場所を -D コマンドライン引数として渡し、次のように使用することです。

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

于 2013-09-03T09:29:48.690 に答える
2

を呼び出し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));
于 2013-09-03T08:55:26.263 に答える
0

config-File を使用して絶対パスを設定するか、jar ファイルへの相対パスにそのディレクトリを作成できます。

于 2013-09-03T08:56:00.890 に答える