Java Code Examples for java.lang.ProcessBuilder.Redirect#PIPE

The following examples show how to use java.lang.ProcessBuilder.Redirect#PIPE . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JavaSubprocessFactory.java    From android-test with Apache License 2.0 6 votes vote down vote up
/**
 * Returns a {@link ProcessBuilder.Redirect} appropriate for the parameters. If a file redirected
 * to exists, deletes the file before redirecting to it.
 */
private Redirect getRedirect(StreamAction action, File file) {
  switch (action) {
    case DISCARD:
      return Redirect.to(new File("/dev/null"));

    case REDIRECT:
      // We need to use Redirect.appendTo() here, because on older Linux kernels writes are
      // otherwise not atomic and might result in lost log messages:
      // https://lkml.org/lkml/2014/3/3/308
      if (file.exists()) {
        file.delete();
      }
      return Redirect.appendTo(file);

    case STREAM:
      return Redirect.PIPE;

    default:
      throw new IllegalStateException();
  }
}
 
Example 2
Source File: JavaSubprocessFactory.java    From bazel with Apache License 2.0 6 votes vote down vote up
/**
 * Returns a {@link java.lang.ProcessBuilder.Redirect} appropriate for the parameters. If a file
 * redirected to exists, deletes the file before redirecting to it.
 */
private Redirect getRedirect(StreamAction action, File file) {
  switch (action) {
    case DISCARD:
      return Redirect.to(new File("/dev/null"));

    case REDIRECT:
      // We need to use Redirect.appendTo() here, because on older Linux kernels writes are
      // otherwise not atomic and might result in lost log messages:
      // https://lkml.org/lkml/2014/3/3/308
      if (file.exists()) {
        file.delete();
      }
      return Redirect.appendTo(file);

    case STREAM:
      return Redirect.PIPE;

    default:
      throw new IllegalStateException();
  }
}
 
Example 3
Source File: CommandExecutor.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
RedirectInfo() {
    this.hasRedirects = false;
    this.inputRedirect = Redirect.PIPE;
    this.outputRedirect = Redirect.PIPE;
    this.errorRedirect = Redirect.PIPE;
    this.mergeError = false;
}
 
Example 4
Source File: SimpleProcessManager.java    From Singularity with Apache License 2.0 4 votes vote down vote up
public List<String> runCommand(
  final List<String> command,
  final Redirect redirectOutput,
  final Set<Integer> acceptableExitCodes
)
  throws InterruptedException, ProcessFailedException {
  final ProcessBuilder processBuilder = new ProcessBuilder(command);

  Optional<Integer> exitCode = Optional.empty();

  Optional<OutputReader> reader = Optional.empty();

  String processToString = getCurrentProcessToString();

  try {
    processBuilder.redirectError(Redirect.INHERIT);
    processBuilder.redirectOutput(redirectOutput);

    final Process process = startProcess(processBuilder);

    processToString = getCurrentProcessToString();

    if (redirectOutput == Redirect.PIPE) {
      reader = Optional.of(new OutputReader(process.getInputStream()));
      reader.get().start();
    }

    exitCode = Optional.of(process.waitFor());

    if (reader.isPresent()) {
      reader.get().join();

      if (reader.get().error.isPresent()) {
        throw reader.get().error.get();
      }
    }
  } catch (InterruptedException ie) {
    signalKillToProcessIfActive();

    throw ie;
  } catch (Throwable t) {
    getLog().error("Unexpected exception while running {}", processToString, t);

    signalKillToProcessIfActive();

    throw new RuntimeException(t);
  } finally {
    processFinished(exitCode);
  }

  if (exitCode.isPresent() && !acceptableExitCodes.contains(exitCode.get())) {
    throw new ProcessFailedException(
      String.format(
        "Got unacceptable exit code %s while running %s",
        exitCode,
        processToString
      )
    );
  }

  if (!reader.isPresent()) {
    return Collections.emptyList();
  }

  return reader.get().output;
}