日期:2014-05-20  浏览次数:20627 次

java调用linux命令参数问题
String command = "ps -ef";
Process child = Runtime.getRuntime().exec(command);
InputStream in = child.getInputStream();
int c;
StringBuilder sb = new StringBuilder();
while ((c = in.read()) != -1) {
sb.append((char)c);
}
in.close();
System.out.println(sb.toString());

这样是可以有返回信息的,但是只要把command = "ps -ef";变为"ps -ef|grep java";就没有任何返回信息了,貌似加|参数后就会出现这个问题,求解。

------解决方案--------------------
If an argument contain spaces, it is necessary to use the overload that requires the command and its arguments to be supplied in an array: 

具体你看看这个,也许会有帮助
http://blog.csdn.net/pengchua/archive/2010/03/08/5357607.aspx