问题描述
我正在尝试使用以下代码重定向在 ProcessBuilder 的帮助下启动的进程的输出
I am trying to redirect output of a process started with the help of ProcessBuilder using following code
ProcessBuilder pb = new ProcessBuilder("/myScript >> /myLogFile 2>&1 <& - &");
Map<String, String> env = pb.environment();
env.clear();
env.put("var1", "val1");
env.put("var2", "val2");
pb.redirectErrorStream(true);
Process p = pb.start();
但它失败了,异常
线程main"中的异常java.io.IOException:无法运行程序"/myScript >>/我的日志文件2>&1 <&- &": java.io.IOException:错误=2,没有这样的文件或目录java.lang.ProcessBuilder.start(ProcessBuilder.java:460)
当我只通过/myScript"时它工作正常
It works fine when I just pass "/myScript"
脚本是 perl,关于它为什么失败的任何建议/评论?
Script is perl, any suggestions/coments on why it is failing?
我尝试将所有这些作为单独的参数传递,例如 new ProcessBuilder("/myScript",">>","/myLogFile")
,它会执行但不会重定向到日志文件,也不需要 envVars.
I tried passing all of them as seperate arguments like new ProcessBuilder("/myScript",">>","/myLogFile")
, it executes but it does not redirect to log file and also it does not take envVars.
推荐答案
ProcessBuilder
.将您的命令放入 shell 脚本中并执行它,如下所示 here一>.或者,使用 bash -c
,如图所示 此处.
Shell redirection operators are unknown to ProcessBuilder
. Put your command in a shell script and execute it, as shown here. Alternatively, use bash -c
, as shown here.
这篇关于ProcessBuilder 重定向输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!