Java exec execGetOutput(String[] command, String[] env)

Here you can find the source of execGetOutput(String[] command, String[] env)

Description

exec Get Output

License

Apache License

Declaration

public static String execGetOutput(String[] command, String[] env) throws IOException, InterruptedException 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {
    public static String execGetOutput(String[] command, String[] env) throws IOException, InterruptedException {
        //getLogger().info("Calling: " + commandMessage);

        //ProcessBuilder pb = new ProcessBuilder(command);
        //pb.redirectErrorStream(true);

        //Process p = pb.start();
        //System.out.println("executing: "+ command[0]+command[1]);

        Process p;// ww  w.  j a v a  2s . c  o  m
        p = Runtime.getRuntime().exec(command, env);

        StringBuffer outputBuf = new StringBuffer();
        BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
        String line = reader.readLine();

        while (line != null) {
            outputBuf.append(line);
            outputBuf.append("\n");
            //getLogger().info(line);
            //System.out.println(line);
            line = reader.readLine();
        }

        // Check for failure
        try {
            if (p.waitFor() != 0) {
                String commandMessage = "";
                for (String part : command) {
                    commandMessage += part + " ";
                }
                String error = "Error executing: " + commandMessage + ". With exit code = " + p.exitValue()
                        + " and output: " + outputBuf;
                System.out.println(error);
                //getLogger().severe(error);
                //throw (new SlipStreamClientException(outputBuf.toString()));
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
            //throw (new SlipStreamInternalException(e));
        } finally {
            reader.close();
        }
        return outputBuf.toString();
    }
}

Related

  1. execCommand(String command)
  2. execCommand(String[] cmd)
  3. execCommand(String[] cmd)
  4. execCommandLineUtility(String cmd)
  5. execGenericCommand(String[] command, String redirectOutput)
  6. execHostName(String execCommand)
  7. execIt(String cmd)
  8. execJAR(String jarPath, String vmArgs, String appArgs, String workDir)
  9. execLocalhostCmd(String cmd)