我想從運行Java代碼此命令並期待一個文件與1個襯墊代碼生成的Java代碼執行shell命令:的Java:從不工作
cut -d , -f 2 /online/data/test/output/2Zip/brita_ids-*.csv | sort -u | tr -d '\n' | sha512sum > /online/data/test/output/file_name.txt
這cmd爲正常,當我運行從cmd行,但我的Java代碼錯了,我很難找出困難,我沒有看到預期的文件正在生成。 任何線索可能發生在這裏?
這裏是我的代碼生成文件:
public String executeCommand(String command) {
StringBuffer output = new StringBuffer();
Process p;
try {
LOG.info("Executing cmd : " + command );
p = Runtime.getRuntime().exec(command);
p.waitFor();
BufferedReader reader =
new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = "";
while ((line = reader.readLine())!= null)
{
output.append(line + "\n");
}
} catch (Exception e) {
e.printStackTrace();
LOG.error("Error in executing cmd : " + command + " \nError : " + e.getMessage());
}
return output.toString();
}
在此先感謝。
什麼是錯誤您收到? – Shriram
這可能會更好1)使用ProcessBuilder和2)爲您的命令使用一個字符串集合,而不是一個字符串。請注意,您不顯示如何調用此方法。 –
@patty使用'getErrorStream()'讀取錯誤' –