Java exec exec(String cmd)

Here you can find the source of exec(String cmd)

Description

exec

License

Apache License

Declaration

public static String[] exec(String cmd) throws Exception 

Method Source Code


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

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

public class Main {
    public static String[] exec(String cmd) throws Exception {
        Process p = Runtime.getRuntime().exec(cmd);
        p.waitFor();/*from   w  w  w . ja  v a 2s. com*/
        BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
        String s = null;
        StringBuilder sbout = new StringBuilder();
        while ((s = stdInput.readLine()) != null) {
            sbout.append(s);
        }
        StringBuilder sberr = new StringBuilder();
        BufferedReader stdErrIn = new BufferedReader(new InputStreamReader(p.getErrorStream()));
        while ((s = stdErrIn.readLine()) != null) {
            sberr.append(s);
        }
        String[] ret = new String[2];
        ret[0] = sbout.toString();
        ret[1] = sberr.toString();
        return ret;
    }
}

Related

  1. exec(String args)
  2. exec(String cmd)
  3. exec(String cmd)
  4. exec(String cmd)
  5. exec(String cmd)
  6. exec(String cmd)
  7. exec(String cmd, File dir)
  8. exec(String command)
  9. exec(String command)