下面列出了java.lang.ProcessBuilder.Redirect#PIPE 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* 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();
}
}
/**
* 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();
}
}
RedirectInfo() {
this.hasRedirects = false;
this.inputRedirect = Redirect.PIPE;
this.outputRedirect = Redirect.PIPE;
this.errorRedirect = Redirect.PIPE;
this.mergeError = false;
}
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;
}