日期:2014-05-16  浏览次数:20567 次

java 调用 linux命令 得到返回值

样例代码如下:

String[] cmd = new String[]{"/bin/sh","-c", " ps -ef"};

Process ps = Runtime.getRuntime().exec(cmd);

BufferedReader br = new BufferedReader(new InputStreamReader(ps.getInputStream()));
StringBuffer sb = new StringBuffer();
String line;
while ((line = br.readLine()) != null) {
sb.append(line).append("\n");
}
String result = sb.toString();

System.out.println(result);

如果需要修改命令的话只需要更改String数组的最后一个元素的值