import java.io.*;

public class ProcessRunner {

  public static String execute(String cmd) throws IOException, InterruptedException {
    Process proc = Runtime.getRuntime().exec(cmd);
    InputStream stdout = proc.getInputStream();
    InputStream stderr = proc.getErrorStream();
    byte[] iobuf = new byte[1024];
    ByteArrayOutputStream bout = new ByteArrayOutputStream(8192);
    while ( isAlive(proc) ) {
      if ( stdout.available() != 0 ) {
        int len = stdout.read(iobuf);
        if ( len == -1 ) break;
        bout.write(iobuf, 0, len);
      }else if ( stderr.available() > 0 ) {
        stderr.read(iobuf);
      }else {
        Thread.sleep(15L);
      }
    }
    return new String(bout.toByteArray());
  }
  private static boolean isAlive(Process proc) {
    try {
      proc.exitValue();
    }catch ( IllegalThreadStateException e ) { return true; }
    return false;
  }
}