Java exec executeCommand(String command, boolean waitForResponse)

Here you can find the source of executeCommand(String command, boolean waitForResponse)

Description

execute Command

License

Open Source License

Declaration

public static String executeCommand(String command,
            boolean waitForResponse) 

Method Source Code

//package com.java2s;
// Released under the Apache License, Version 2.0

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringWriter;
import java.io.Writer;

public class Main {
    private static final boolean DEBUG = false;

    public static String executeCommand(String command,
            boolean waitForResponse) {

        String response = "";
        ProcessBuilder pb = null;

        pb = new ProcessBuilder("bash", "-c", command);
        pb.redirectErrorStream(true);/*w  w w . j av  a2s .  co m*/

        if (DEBUG) {
            System.out.println("Command: " + command);
        }

        try {
            Process shell = pb.start();
            if (waitForResponse) {

                // To capture output from the shell
                InputStream shellIn = shell.getInputStream();

                // Wait for the shell to finish and get the return code
                int shellExitStatus = shell.waitFor();

                if (DEBUG) {
                    System.out.println("Exit status = " + shellExitStatus);
                }

                response = convertStreamToStr(shellIn);
                shellIn.close();
            }

        } catch (IOException e) {
            System.out
                    .println("Error occured while executing command. Error Description: "
                            + e.getMessage());
        } catch (InterruptedException e) {
            System.out
                    .println("Error occured while executing command. Error Description: "
                            + e.getMessage());
        }

        return response;
    }

    public static String convertStreamToStr(InputStream is)
            throws IOException {

        if (is != null) {
            Writer writer = new StringWriter();

            char[] buffer = new char[1024];
            try {
                Reader reader = new BufferedReader(new InputStreamReader(
                        is, "UTF-8"));
                int n;
                while ((n = reader.read(buffer)) != -1) {
                    writer.write(buffer, 0, n);
                }
            } finally {
                is.close();
            }
            return writer.toString();
        } else {
            return "";
        }
    }
}

Related

  1. executeCommand(String command)
  2. executeCommand(String command)
  3. executeCommand(String command)
  4. executeCommand(String command)
  5. executeCommand(String command)
  6. executeCommand(String[] command, boolean wait)
  7. executeCommand1(boolean isProcessBuilder, String command)
  8. executeCommand2(boolean isProcessBuilder, String command)
  9. executeCommandAndExtractStdOut(String cmd)