Java Shell Command runCommand(String program, ArrayList args)

Here you can find the source of runCommand(String program, ArrayList args)

Description

Helper function to run a system command and return the stdout / stderr as a string

License

Apache License

Parameter

Parameter Description
program a parameter
args a parameter

Exception

Parameter Description
IOException an exception
InterruptedException an exception

Declaration

protected static String runCommand(String program,
        ArrayList<String> args) throws InterruptedException 

Method Source Code

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

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Map;

public class Main {
    public static StringBuilder outputBuilder;
    private static String commandPath = "";
    private static String additionalEnv = "";
    public static int exitValue;

    /**/*from  w  w w . j  ava 2 s.co m*/
     * Helper function to run a system command and return the stdout / stderr as
     * a string
     * 
     * @param program
     * @param args
     * @return
     * @throws IOException
     * @throws InterruptedException
     */
    protected static String runCommand(String program,
            ArrayList<String> args) throws InterruptedException {

        ArrayList<String> command = new ArrayList<String>();
        command.add(program);
        if (args != null) {
            for (String arg : args) {
                command.add(arg);
            }
        }
        System.out.println("RUNNING COMMAND :" + join(command, " "));

        ProcessBuilder builder = new ProcessBuilder(command);
        // we need to specify environment variables
        Map<String, String> environment = builder.environment();

        String ldLibPath = commandPath;
        if (additionalEnv.length() > 0) {
            ldLibPath += ":" + additionalEnv;
        }

        environment.put("LD_LIBRARY_PATH", ldLibPath);
        try {
            Process handle = builder.start();

            InputStream stdErr = handle.getErrorStream();
            InputStream stdOut = handle.getInputStream();

            // TODO: we likely don't need this
            // OutputStream stdIn = handle.getOutputStream();

            outputBuilder = new StringBuilder();
            byte[] buff = new byte[4096];

            // TODO: should we read both of these streams?
            // if we break out of the first loop is the process terminated?

            // read stderr
            for (int n; (n = stdErr.read(buff)) != -1;) {
                outputBuilder.append(new String(buff, 0, n));
            }
            // read stdout
            for (int n; (n = stdOut.read(buff)) != -1;) {
                outputBuilder.append(new String(buff, 0, n));
            }

            stdOut.close();
            stdErr.close();

            // TODO: stdin if we use it.
            // stdIn.close();

            // the process should be closed by now?

            handle.waitFor();

            handle.destroy();

            exitValue = handle.exitValue();
            // print the output from the command
            System.out.println(outputBuilder.toString());
            System.out.println("Exit Value : " + exitValue);
            outputBuilder.append("Exit Value : " + exitValue);

            return outputBuilder.toString();
        } catch (IOException e) {
            exitValue = 5;
            return e.getMessage();
            // throw e;
        }
    }

    private static String join(ArrayList<String> list, String joinChar) {
        StringBuilder sb = new StringBuilder();
        int i = 0;
        int size = list.size();
        for (String part : list) {
            i++;
            sb.append(part);
            if (i != size) {
                sb.append(joinChar);
            }
        }
        return sb.toString();
    }
}

Related

  1. runCommand(String cmd)
  2. runCommand(String command)
  3. runCommand(String command)
  4. runCommand(String command, String args, String file)
  5. runCommand(String command[])
  6. runCommand(String s)
  7. runCommand(String[] args)
  8. runCommand(String[] args)
  9. runCommand(String[] cmdArray, String extraPath, String libPath)