【问题标题】:How to use pipes in a java Runtime.exec [duplicate]如何在 java Runtime.exec 中使用管道 [重复]
【发布时间】:2015-08-13 21:46:53
【问题描述】:

此命令行的输出为 null。

Process result = Runtime.getRuntime().exec(new String[]{"/usr/bin/find",baseDir+"/..","-type","f","|","/usr/bin/grep",filter1,"|","/usr/bin/grep",filter2,"|","/usr/bin/wc","-l"});
result.waitFor();
BufferedReader echo = new BufferedReader(new InputStreamReader(result.getInputStream()));
writer.print(echo.readLine());
echo.close();

是管道“|”吗?

【问题讨论】:

标签: java pipe runtime.exec


【解决方案1】:

要获得像|这样的shell命令,使用/bin/bash作为exec的第一个参数,-c作为第二个参数,整个字符串(包括find,它的参数和管道等)作为第三个。

Process result = Runtime.getRuntime().exec(new String[]{"/bin/bash", "-c", "/usr/bin/find " + baseDir+"/.. -type f | /usr/bin/grep " +filter1 + "| /usr/bin/grep "+filter2+" | /usr/bin/wc -l"});

【讨论】:

    猜你喜欢
    • 2011-11-05
    • 2016-08-22
    • 1970-01-01
    • 1970-01-01
    • 2021-09-24
    • 1970-01-01
    • 1970-01-01
    • 2011-10-04
    相关资源
    最近更新 更多