日期:2014-05-20 浏览次数:20817 次
Process p = Runtime.getruntime.exec(cmd); BufferReader br = new BufferedRead(new InputStreamReader(p.getInputStream())); for (String buf=br.readLine(); buf != null; buf=br.readLine()) { System.out.println(buf); }
------解决方案--------------------
import java.io.*; import java.util.regex.*; public class GetPID { public static void main(String[] args) { Process process = null; BufferedReader br = null; try { process = Runtime.getRuntime().exec("ps -ef"); br = new BufferedReader(new InputStreamReader( process.getInputStream()), 1024); String line = null; Pattern pattern = Pattern.compile("\\S*\\s*([0-9]*).*"); Matcher matcher = null; System.out.println("PID list: "); while ((line = br.readLine()) != null) { matcher = pattern.matcher(line); if (matcher.find()) { System.out.println(matcher.group(1)); } } } catch (IOException e) { e.printStackTrace(); } finally { if (process != null) { int retval = 0; try { retval = process.waitFor(); System.out.println("retval = " + retval); } catch (InterruptedException e) { e.printStackTrace(); } finally { if (br != null) { try { br.close(); } catch (IOException e) { e.printStackTrace(); } } } } } } }