Java exec execPrint(final boolean out, final String... command)

Here you can find the source of execPrint(final boolean out, final String... command)

Description

Executes the command, optionally copying output to standard output.

License

Open Source License

Parameter

Parameter Description
out whether the output should be shown on stdout
command the command and arguments to run

Exception

Parameter Description
IOException if the program cannot be launched

Return

the exit code of the program

Declaration

public static int execPrint(final boolean out, final String... command)
        throws IOException 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.io.*;

public class Main {
    /**/* www.j  a  v a 2s .c o  m*/
     * Executes the command, optionally copying output to standard output.
     * 
     * @param out whether the output should be shown on stdout
     * @param command the command and arguments to run
     * @return the exit code of the program
     * @throws IOException if the program cannot be launched
     */
    public static int execPrint(final boolean out, final String... command)
            throws IOException {
        final ProcessBuilder builder = new ProcessBuilder(command);
        builder.redirectErrorStream(true);
        final Process p = builder.start();
        final InputStream is = p.getInputStream();
        int c;
        // Read-print loop
        while ((c = is.read()) >= 0)
            if (out) {
                System.out.write(c);
                System.out.flush();
            }
        is.close();
        return p.exitValue();
    }
}

Related

  1. execIt(String cmd)
  2. execJAR(String jarPath, String vmArgs, String appArgs, String workDir)
  3. execLocalhostCmd(String cmd)
  4. exeCmdByOs(String cmd)
  5. execName()
  6. execProcess(String process)
  7. execProcess(String[] cmdline, final long timeout)
  8. Execption2Strings(boolean rep, Throwable... execptions)
  9. execScript(File shellScriptFile, String[] scriptCommand, PrintWriter execLog)