Here you can find the source of readLinesFromCommand(String command[], List
public static int readLinesFromCommand(String command[], List<String> buffer)
//package com.java2s; //License from project: Open Source License import java.util.Arrays; import java.util.List; import java.io.Closeable; import java.io.IOException; import java.io.BufferedReader; import java.io.InputStreamReader; public class Main { public static int readLinesFromCommand(String command[], List<String> buffer) { if (command == null || buffer == null) { return -2; }// w w w .j a v a2 s . c o m Process cmdProc = null; try { cmdProc = Runtime.getRuntime().exec(command); } catch (IOException e) { System.out.printf("`%s` failed\n", Arrays.toString(command)); e.printStackTrace(); return -1; } BufferedReader cmdOutput = new BufferedReader( new InputStreamReader(cmdProc.getInputStream())); int returnCode = -1; try { returnCode = cmdProc.waitFor(); } catch (InterruptedException e) { System.out.printf("Interrupted while waiting for `%s`\n", Arrays.toString(command)); e.printStackTrace(); return -1; } String line = null; try { line = cmdOutput.readLine(); } catch (IOException e) { System.out.printf("reading `%s` output failed\n", Arrays.toString(command)); return -1; } while (line != null) { buffer.add(line); try { line = cmdOutput.readLine(); } catch (IOException e) { System.out.printf("reading `%s` output failed\n", Arrays.toString(command)); return -1; } } if (cmdProc != null) { boolean waited = false; while (!waited) { try { cmdProc.waitFor(); waited = true; } catch (InterruptedException e) { } } if (cmdProc != null) { close(cmdProc.getOutputStream()); close(cmdProc.getInputStream()); close(cmdProc.getErrorStream()); cmdProc.destroy(); } } return returnCode; } private static void close(Closeable c) { if (c != null) { try { c.close(); } catch (IOException e) { // ignored } } } }