java.lang.ProcessBuilder.redirectOutput()方法的使用及代码示例

x33g5p2x  于2022-01-25 转载在 其他  
字(10.9k)|赞(0)|评价(0)|浏览(228)

本文整理了Java中java.lang.ProcessBuilder.redirectOutput()方法的一些代码示例,展示了ProcessBuilder.redirectOutput()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。ProcessBuilder.redirectOutput()方法的具体详情如下:
包路径:java.lang.ProcessBuilder
类名称:ProcessBuilder
方法名:redirectOutput

ProcessBuilder.redirectOutput介绍

暂无

代码示例

代码示例来源:origin: apache/incubator-gobblin

public Process start(final List<String> command)
   throws IOException {
  final ProcessBuilder processBuilder = new ProcessBuilder(command);
  processBuilder.redirectOutput(ProcessBuilder.Redirect.INHERIT);
  final Process process = processBuilder.start();
  return process;
 }
}

代码示例来源:origin: stackoverflow.com

ProcessBuilder pb = new ProcessBuilder("yourcommand");
pb.redirectOutput(Redirect.INHERIT);
pb.redirectError(Redirect.INHERIT);
Process p = pb.start();

代码示例来源:origin: jphp-group/jphp

@Signature
public WrapProcess redirectOutputToFile(File file) {
  processBuilder.redirectOutput(file);
  return this;
}

代码示例来源:origin: jphp-group/jphp

@Signature
public WrapProcess redirectOutputToInherit() {
  processBuilder.redirectOutput(ProcessBuilder.Redirect.INHERIT);
  return this;
}

代码示例来源:origin: jphp-group/jphp

@Signature
public WrapProcess redirectOutputToPipe() {
  processBuilder.redirectOutput(ProcessBuilder.Redirect.PIPE);
  return this;
}

代码示例来源:origin: prestodb/presto

public static Pager create(List<String> command)
{
  try {
    Process process = new ProcessBuilder()
        .command(command)
        .redirectOutput(ProcessBuilder.Redirect.INHERIT)
        .redirectError(ProcessBuilder.Redirect.INHERIT)
        .start();
    return new Pager(process.getOutputStream(), process);
  }
  catch (IOException e) {
    System.err.println("ERROR: failed to open pager: " + e.getMessage());
    return createNullPager();
  }
}

代码示例来源:origin: stackoverflow.com

ProcessBuilder builder = new ProcessBuilder("sh", "somescript.sh");
builder.redirectOutput(new File("out.txt"));
builder.redirectError(new File("out.txt"));
Process p = builder.start(); // may throw IOException

代码示例来源:origin: Alluxio/alluxio

/**
 * Starts the process.
 */
public synchronized void start() throws IOException {
 Preconditions.checkState(mProcess == null, "Process is already running");
 String java = PathUtils.concatPath(System.getProperty("java.home"), "bin", "java");
 String classpath = System.getProperty("java.class.path");
 List<String> args = new ArrayList<>(Arrays.asList(java, "-cp", classpath));
 for (Entry<PropertyKey, String> entry : mConf.entrySet()) {
  args.add(String.format("-D%s=%s", entry.getKey().toString(), entry.getValue()));
 }
 args.add(mClazz.getCanonicalName());
 ProcessBuilder pb = new ProcessBuilder(args);
 pb.redirectError(mOutFile);
 pb.redirectOutput(mOutFile);
 mProcess = pb.start();
}

代码示例来源:origin: ming1016/study

@Nullable
public Process startInterpreter(String pythonExe) {
  Process p;
  try {
    ProcessBuilder builder = new ProcessBuilder(pythonExe, "-i", jsonizer);
    builder.redirectErrorStream(true);
    builder.redirectError(new File(parserLog));
    builder.redirectOutput(new File(parserLog));
    builder.environment().remove("PYTHONPATH");
    p = builder.start();
  } catch (Exception e) {
    _.msg("Failed to start: " + pythonExe);
    return null;
  }
  return p;
}

代码示例来源:origin: googleapis/google-cloud-java

ProcessBuilder getBuilder() {
 ProcessBuilder builder = new ProcessBuilder(command);
 if (redirectOutputToNull) {
  builder.redirectOutput(new File(nullFilename));
 }
 if (directory != null) {
  builder.directory(directory.toFile());
 }
 if (redirectErrorStream) {
  builder.redirectErrorStream(true);
 }
 if (redirectErrorInherit) {
  builder.redirectError(ProcessBuilder.Redirect.INHERIT);
 }
 return builder;
}

代码示例来源:origin: apache/hive

private int runPackagePy(String[] args, Path tmpDir, Path scriptParent,
  String version, String outputDir) throws IOException, InterruptedException {
 Path scriptPath = new Path(new Path(scriptParent, "yarn"), "package.py");
 List<String> scriptArgs = new ArrayList<>(args.length + 7);
 scriptArgs.add("python");
 scriptArgs.add(scriptPath.toString());
 scriptArgs.add("--input");
 scriptArgs.add(tmpDir.toString());
 scriptArgs.add("--output");
 scriptArgs.add(outputDir);
 scriptArgs.add("--javaChild");
 for (String arg : args) {
  scriptArgs.add(arg);
 }
 LOG.debug("Calling package.py via: " + scriptArgs);
 ProcessBuilder builder = new ProcessBuilder(scriptArgs);
 builder.redirectError(ProcessBuilder.Redirect.INHERIT);
 builder.redirectOutput(ProcessBuilder.Redirect.INHERIT);
 builder.environment().put("HIVE_VERSION", version);
 return builder.start().waitFor();
}

代码示例来源:origin: Alluxio/alluxio

/**
 * Copies the work directory to the artifacts folder.
 */
public synchronized void saveWorkdir() throws IOException {
 Preconditions.checkState(mState == State.STARTED,
   "cluster must be started before you can save its work directory");
 ARTIFACTS_DIR.mkdirs();
 File tarball = new File(mWorkDir.getParentFile(), mWorkDir.getName() + ".tar.gz");
 // Tar up the work directory.
 ProcessBuilder pb =
   new ProcessBuilder("tar", "-czf", tarball.getName(), mWorkDir.getName());
 pb.directory(mWorkDir.getParentFile());
 pb.redirectOutput(Redirect.appendTo(TESTS_LOG));
 pb.redirectError(Redirect.appendTo(TESTS_LOG));
 Process p = pb.start();
 try {
  p.waitFor();
 } catch (InterruptedException e) {
  Thread.currentThread().interrupt();
  throw new RuntimeException(e);
 }
 // Move tarball to artifacts directory.
 File finalTarball = new File(ARTIFACTS_DIR, tarball.getName());
 FileUtils.moveFile(tarball, finalTarball);
 LOG.info("Saved cluster {} to {}", mClusterName, finalTarball.getAbsolutePath());
}

代码示例来源:origin: jmxtrans/jmxtrans

@Override
@IgnoreJRERequirement // ProcessBuilder.inheritIO() was introduced in Java 7. As this is only used in test, let's ignore it.
protected void before() throws Throwable {
  List<String> command = new ArrayList<String>(asList("java", "-cp", getCurrentClasspath()));
  for(Map.Entry<Object, Object> property: properties.entrySet()) {
    command.add("-D"+property.getKey()+"="+property.getValue());
  }
  command.add(appClass.getCanonicalName());
  if (appArgs != null) {
    command.addAll(asList(appArgs));
  }
  ProcessBuilder processBuilder = new ProcessBuilder().command(command)
    .inheritIO();
  if (outputFile != null) {
    processBuilder.redirectOutput(outputFile);
  }
  app = processBuilder.start();
}

代码示例来源:origin: eclipse-vertx/vert.x

private void executeUserCommand(Handler<Void> onCompletion) {
 if (cmd != null) {
  try {
   List<String> command = new ArrayList<>();
   if (ExecUtils.isWindows()) {
    ExecUtils.addArgument(command, "cmd");
    ExecUtils.addArgument(command, "/c");
   } else {
    ExecUtils.addArgument(command, "sh");
    ExecUtils.addArgument(command, "-c");
   }
   // Do not add quote to the given command:
   command.add(cmd);
   final Process process = new ProcessBuilder(command)
     .redirectError(ProcessBuilder.Redirect.INHERIT)
     .redirectOutput(ProcessBuilder.Redirect.INHERIT)
     .start();
   int status = process.waitFor();
   LOGGER.info("User command terminated with status " + status);
  } catch (Throwable e) {
   LOGGER.error("Error while executing the on-redeploy command : '" + cmd + "'", e);
  }
 }
 onCompletion.handle(null);
}

代码示例来源:origin: neo4j/neo4j

private static Process start( boolean inheritOutput, String... args )
{
  ProcessBuilder builder = new ProcessBuilder( args );
  if ( inheritOutput )
  {
    // We can not simply use builder.inheritIO here because
    // that will also inherit input which will be closed in case of background execution of main process.
    // Closed input stream will cause immediate exit from a subprocess liveloop.
    // And we use background execution in scripts and on CI server.
    builder.redirectError( ProcessBuilder.Redirect.INHERIT )
        .redirectOutput( ProcessBuilder.Redirect.INHERIT );
  }
  try
  {
    return builder.start();
  }
  catch ( IOException e )
  {
    throw new RuntimeException( "Failed to start sub process", e );
  }
}

代码示例来源:origin: apache/incubator-pinot

private void runAdminCommand(String... args)
   throws Exception {
  ArrayList<String> commandLine = Lists
    .newArrayList("java", "-cp", "pinot-tools/target/pinot-tool-launcher-jar-with-dependencies.jar",
      "org.apache.pinot.tools.admin.PinotAdministrator");

  commandLine.addAll(Lists.newArrayList(args));

  LOGGER.info("Running command " + Joiner.on(" ").join(commandLine));

  Process process =
    new ProcessBuilder(commandLine.toArray(new String[0])).redirectOutput(ProcessBuilder.Redirect.INHERIT)
      .redirectError(ProcessBuilder.Redirect.INHERIT).start();
  int returnCode = process.waitFor();
  assertEquals(returnCode, 0);
 }
}

代码示例来源:origin: eclipse-vertx/vert.x

if (redirect) {
 builder.redirectError(ProcessBuilder.Redirect.INHERIT);
 builder.redirectOutput(ProcessBuilder.Redirect.INHERIT);

代码示例来源:origin: apache/incubator-pinot

private Process runAdministratorCommand(String[] args) {
 String classpath = System.getProperty("java.class.path");
 List<String> completeArgs = new ArrayList<>();
 completeArgs.add("java");
 completeArgs.add("-Xms4G");
 completeArgs.add("-Xmx4G");
 completeArgs.add("-cp");
 completeArgs.add(classpath);
 completeArgs.add(PinotAdministrator.class.getName());
 completeArgs.addAll(Arrays.asList(args));
 try {
  Process process = new ProcessBuilder(completeArgs).redirectError(ProcessBuilder.Redirect.INHERIT).
    redirectOutput(ProcessBuilder.Redirect.INHERIT).start();
  synchronized (_processes) {
   _processes.add(process);
  }
  return process;
 } catch (IOException e) {
  throw new RuntimeException(e);
 }
}

代码示例来源:origin: org.apache.logging.log4j/log4j-core

public static void runTest(final Class<?> cls) throws Exception {
  final String javaHome = System.getProperty("java.home");
  final String javaBin = javaHome + File.separator + "bin" + File.separator + "java";
  final String classpath = System.getProperty("java.class.path");
  final String javaagent = "-javaagent:" + agentJar();
  final File tempFile = File.createTempFile("allocations", ".txt");
  tempFile.deleteOnExit();
  final ProcessBuilder builder = new ProcessBuilder( //
      javaBin, javaagent, "-cp", classpath, cls.getName());
  builder.redirectError(ProcessBuilder.Redirect.to(tempFile));
  builder.redirectOutput(ProcessBuilder.Redirect.to(tempFile));
  final Process process = builder.start();
  process.waitFor();
  process.exitValue();
  final String text = new String(Files.readAllBytes(tempFile.toPath()));
  final List<String> lines = Files.readAllLines(tempFile.toPath(), Charset.defaultCharset());
  final String className = cls.getSimpleName();
  assertEquals(text, "FATAL o.a.l.l.c." + className + " [main] value1 {aKey=value1, key2=value2, prop1=value1, prop2=value2} This message is logged to the console",
      lines.get(0));
  for (int i = 1; i < lines.size(); i++) {
    final String line = lines.get(i);
    assertFalse(i + ": " + line + Strings.LINE_SEPARATOR + text, line.contains("allocated") || line.contains("array"));
  }
}

代码示例来源:origin: ehcache/ehcache3

private static void installKit(String diskPrefix) {
 try {
  Process process = new ProcessBuilder(diskPrefix + "../../gradlew", "copyServerLibs")
   .redirectError(ProcessBuilder.Redirect.INHERIT)
   .redirectOutput(ProcessBuilder.Redirect.INHERIT)
   .start();
  int status = process.waitFor();
  assertThat(status).isZero();
 } catch (IOException e) {
  fail("Failed to start gradle to install kit", e);
 } catch (InterruptedException e) {
  fail("Interrupted while installing kit", e);
 }
}

相关文章