Javaアプリケーションでruntime.getruntime.execを使用しようとしています。Runtime.getRuntime.exec()エラーコード
かなり長い間、私は別のコマンドを実行しようとしていましたが、ファイルまたはディレクトリが存在しないことを意味するエラーコード2が続きました。テストするために、基本コマンドを渡してエラーコード1を取得しようとしました。なぜこれを取得していますか、エラーコード1は何を意味しますか?ここで
は私のコードです:ここでは
private String executeCommand(String command) {
logger.info("executing command : " + command);
String result = null;
try {
Runtime rt = Runtime.getRuntime();
Process pr = rt.exec(command);
BufferedReader input = new BufferedReader(new InputStreamReader(pr.getInputStream()));
BufferedReader stdError = new BufferedReader(new InputStreamReader(pr.getErrorStream()));
String line = null;
while ((line = input.readLine()) != null) {
result = result + line;
}
while ((line = stdError.readLine()) != null) {
result = result + line;
}
int exitVal = pr.waitFor();
System.out.println("Exited with error code " + exitVal);
} catch (IOException e) {
e.printStackTrace();
} catch (NumberFormatException e) {
e.printStackTrace();
} catch (Throwable e) {
e.printStackTrace();
}
logger.info("This is the result:" + result);
return result;
は、私はそれを呼び出す方法です。ここで
String temp = executeCommand("cd $HOME/my-directory/my-subdirectory");
は私の出力です:
INFO : programname - executing command : cd $HOME/my-directory/my-
subdirectory
Exited with error code 1
INFO : programname - This is the result:null/usr/bin/cd[8]: $HOME/my-
directory/my-subdirectory: not found
ホーム変数を囲み、 'String temp = executeCommand(" cd $ {HOME}/my-directory/my-subdirectory ");' –
変更を実行しないでください。さらに、コマンドラインでそれを単独で実行すると、このコマンドは完全に正常に動作します – iswg
'String temp = executeCommand("/usr/bin/cd $ HOME/my-directory/my-subdirectory ");' –