Exemple #1
0
 /**
  * Checks whether the execution result return value is 0. If not, throws an exception.
  *
  * @param executionResult the process execution result
  * @throws Exception if exit value is different than 0
  */
 public static void checkReturnValue(ExecutionResult executionResult) throws Exception {
   int exitValue = executionResult.getReturnValue();
   if (exitValue != ExecutionResult.OK) {
     throw new Exception(
         "Exit value: "
             + exitValue
             + ", StdErr: "
             + executionResult.getStdErr().toString()
             + ", StdOut: "
             + executionResult.getStdOut().toString());
   }
 }
Exemple #2
0
  /**
   * Runs the process builded by the given process builder.
   *
   * @param processBuilder the builder of the process
   * @return the process execution result
   * @throws Exception if a problem occurs amidst execution of the process
   */
  public static ExecutionResult runProcess(ProcessBuilder processBuilder) throws Exception {

    final Process startedProcess = processBuilder.start();

    Callable<List<String>> stdOutCallable = createStreamCallable(startedProcess.getInputStream());
    Callable<List<String>> stdErrCallable = createStreamCallable(startedProcess.getErrorStream());
    Callable<Integer> waitForCallable = createWaitForCallable(startedProcess);

    ExecutorService executor = Executors.newFixedThreadPool(3);
    Future<List<String>> stdOutFuture = executor.submit(stdOutCallable);
    Future<List<String>> stdErrFuture = executor.submit(stdErrCallable);
    Future<Integer> waitForFuture = executor.submit(waitForCallable);

    ExecutionResult executionResult = new ExecutionResult();

    executionResult.setReturnValue(getWaitForCallableResult(waitForFuture));
    executionResult.setStdOut(getStreamCallableResult(stdOutFuture));
    executionResult.setStdErr(getStreamCallableResult(stdErrFuture));

    executor.shutdownNow();

    return executionResult;
  }