Java exec executeCommand(String command)

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

Description

execute Command

License

Open Source License

Declaration

public static String executeCommand(String command) 

Method Source Code


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

import java.io.BufferedReader;
import java.io.File;

import java.io.InputStreamReader;

public class Main {
    public static String executeCommand(String command) {
        return executeCommand(command, "./");
    }/*w  w w . ja  v  a 2 s . co  m*/

    public static String executeCommand(String[] command) {
        return executeCommand(command, "./");
    }

    public static String executeCommand(String command, String path) {

        StringBuffer output = new StringBuffer();

        Process p;
        try {
            p = Runtime.getRuntime().exec(command, null, new File(path));
            p.waitFor();
            BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));

            String line = "";
            while ((line = reader.readLine()) != null) {
                output.append(line + "\n");
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

        return output.toString();

    }

    public static String executeCommand(String[] command, String path) {

        StringBuffer output = new StringBuffer();

        Process p;
        try {
            p = Runtime.getRuntime().exec(command, null, new File(path));
            p.waitFor();
            BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));

            String line = "";
            while ((line = reader.readLine()) != null) {
                output.append(line + "\n");
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

        return output.toString();

    }
}

Related

  1. executeCommand(String cmd)
  2. executeCommand(String comand)
  3. executeCommand(String command)
  4. executeCommand(String command)
  5. executeCommand(String command)
  6. executeCommand(String command)
  7. executeCommand(String command)
  8. executeCommand(String command, boolean waitForResponse)
  9. executeCommand(String[] command, boolean wait)