24
Process p = Runtime.getRuntime().exec("sh somescript.sh &> out.txt");

Javaを使用してこのコマンドを実行しています。スクリプトは実行されていますが、ストリームをファイルにリダイレクトしていません。さらに、ファイルout.txtが作成されていません。

このスクリプトをシェルで実行すると、問題なく実行されます。

何か案は?

4

2 に答える 2

44

ProcessBuilderを使用してリダイレクトする必要があります。

ProcessBuilder builder = new ProcessBuilder("sh", "somescript.sh");
builder.redirectOutput(new File("out.txt"));
builder.redirectError(new File("out.txt"));
Process p = builder.start(); // may throw IOException
于 2013-04-26T14:29:50.750 に答える
7

When you run a command, there is no shell running and any shell commands or functions are not available. To use something like &> you need a shell. You have one but you are not passing it to it. try instead.

Runtime.getRuntime().exec(new String[] { "sh", "somescript.sh &> out.txt" });
于 2013-04-26T14:25:12.313 に答える