Esempio n. 1
0
  /**
   * Execute the given command and optionally wait and dump the results to standard out
   *
   * @param args command and arguments
   * @param wait true =wait for either completion or timeout time and dump output, false don't wait
   *     and ignore the output.
   * @exception Exception
   */
  private static void execCmdDumpResults(String[] args, boolean wait) throws Exception {
    // We need the process inputstream and errorstream
    ProcessStreamResult prout = null;
    ProcessStreamResult prerr = null;

    System.out.flush();
    bos.flush();

    BufferedOutputStream _bos = bos;
    if (!wait) {
      // not interested in the output, don't expect a huge amount.
      // information will just be written to the byte array in
      // memory and never used.
      _bos = new BufferedOutputStream(new ByteArrayOutputStream());
    }
    // Start a process to run the command
    Process pr = execCmd(args);

    // Note, the timeout handling will only come into effect when we make
    // the Wait() call on ProcessStreamResult.
    prout = new ProcessStreamResult(pr.getInputStream(), _bos, timeoutMinutes);
    prerr = new ProcessStreamResult(pr.getErrorStream(), _bos, timeoutMinutes);

    if (!wait) return;

    // wait until all the results have been processed or if we timed out
    prout.Wait();
    prerr.Wait();
    _bos.flush();
    System.out.flush();
  }
 public static String exec_result(String command) {
   try {
     String result = "";
     Process p = null;
     try {
       p = Runtime.getRuntime().exec(command);
     } catch (IOException e) {
       e.printStackTrace();
     }
     BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
     int count = 0;
     while (!procDone(p)) {
       try {
         String s;
         while ((s = stdInput.readLine()) != null) {
           count++;
           result = result + s + "\n";
         }
       } catch (IOException e) {
       }
     }
     try {
       stdInput.close();
     } catch (IOException e) {
       e.printStackTrace();
     }
     return result;
   } catch (ArrayIndexOutOfBoundsException e) {
   }
   return "";
 }
Esempio n. 3
0
  private String shell_exec(String cmdline) {
    String line = "";
    try {

      // windows
      // Process p = Runtime.getRuntime().exec(cmdline);
      // linux
      Process p = Runtime.getRuntime().exec(new String[] {"/bin/sh", "-c", cmdline});

      BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
      while ((line += input.readLine()) != null) {}
      input.close();
    } catch (Exception err) {
      err.printStackTrace();
    }
    return line;
  }