Java tutorial
//package com.java2s; //License from project: Open Source License import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class Main { /** * Retrieves the output (stdout) of the process represented by the given {@link Process} * object. This method blocks until the process is about to terminate and returns the * complete output as a string. The returned string contains newlines after each output * line. This is also true for the last line and for processes that do not output line * endings, i.e., the string always has a trailing newline except for empty outputs. * Note that the process is likely to have not terminated yet after this method returns. * * @param proc * The process object. * @return The stdout output of the process. May be an empty string. The string always * has a trailing newline if it is not empty. * @throws IOException * If the process output cannot be read. */ public static String getProcessOutput(final Process proc) throws IOException { try (final InputStreamReader isr = new InputStreamReader(proc.getInputStream()); final BufferedReader r = new BufferedReader(isr)) { final StringBuilder sb = new StringBuilder(); String line; while ((line = r.readLine()) != null) { sb.append(line); sb.append("\n"); } return sb.toString(); } catch (final IOException e) { throw e; } } }