How do I redirect output of a shell script using Java?(如何使用Java重定向shell脚本的输出?)
问题描述
我正在尝试使用Java在Linux(Ubuntu)中执行shell脚本,并尝试将输出重定向到另一个文件。
String cmd[] = {"sh", "-c", "my_dir/script.sh > new_dir/out.txt"};
Process pb = Runtime.getRuntime().exec(cmd);
但是,该用户不能正常工作。我无法将输出存储在out.txt文件中。 有没有人能建议一种正确做这件事的方法?
推荐答案
这对我很有效。 使用Runtime.exec()
Process p = Runtime.getRuntime().exec("my_dir/script.sh > new_dir/out.txt");
// Wait for execution completion
p.waitFor();
这篇关于如何使用Java重定向shell脚本的输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!