【发布时间】:2020-10-28 09:00:00
【问题描述】:
我想构建一个像 Jenkins 终端这样的应用程序:
而我用JavaRuntime.getRuntime().exec(command)执行命令,发现输出不全。
textshell.sh:
# textshell.sh
echo "wwwwwww";
sleep 2
ls
例如:
当我在我的 mac 终端sh -x testshell.sh 中执行 textshell.sh 时,输出:
+ echo wwwwwww
wwwwwww
+ sleep 2
+ ls
testshell.sh
但是当我通过 java Java Runtime.getRuntime().exec("sh -x testshell.sh") 执行时,输出:
wwwwwww
testshell.sh
shell args -x 似乎没用
我该如何解决?
【问题讨论】:
-
似乎“+”前缀行被打印到 stderr。确保同时使用(并显示)标准输出和标准错误流。
-
尝试运行这个
Runtime.getRuntime().exec(new String[]{"sh", "-x", "testshell.sh"}) -
阅读标准错误流并修复问题,非常感谢