2015-08-13 65 views
0

我得到null作爲此命令行的輸出。如何在java Runtime.exec中使用管道

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

http://stackoverflow.com/questions/5928225/how-to-make-pipes-work-with-runtime-exec –

回答

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"});