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.*;

public class Main {
    public static String executeCommand(String command) {
        try {/* w  ww  . jav  a 2  s  .c  om*/
            Runtime r = Runtime.getRuntime();
            String[] cmd = { "/bin/sh", "-c", command };
            Process p = r.exec(cmd);
            p.waitFor();
            return inputStreamToString(p.getInputStream());
        } catch (IOException ex) {
            return ex.toString();
        } catch (InterruptedException ex) {
            return ex.toString();
        }
    }

    public static String inputStreamToString(InputStream inputStream) {
        BufferedReader b = new BufferedReader(new InputStreamReader(inputStream));
        String line;
        StringBuilder all = new StringBuilder();
        try {
            while ((line = b.readLine()) != null) {
                all.append(line);
            }
        } catch (IOException e) {
            return e.toString();
        }
        return all.toString();
    }
}

Related

  1. executeCommand(List cmdArray)
  2. executeCommand(List command)
  3. executeCommand(String cmd)
  4. executeCommand(String cmd)
  5. executeCommand(String comand)
  6. executeCommand(String command)
  7. executeCommand(String command)
  8. executeCommand(String command)
  9. executeCommand(String command)