如何在java Runtime.exec

时间:2015-08-13 21:46:54

标签: java pipe runtime.exec

我得到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();

是管道“|”吗?

1 个答案:

答案 0 :(得分:2)

可能。要获取|之类的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"});