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

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

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

ProcessBuilder.command介绍

[英]Returns this process builder's current program and arguments. Note that the returned list is not a copy and modifications to it will change the state of this instance.
[中]返回此process builder的当前程序和参数。请注意,返回的列表不是副本,对其进行修改将更改此实例的状态。

代码示例

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

public ProcessRunner command(String... command) {
  builder.command(command);
  return this;
}

代码示例来源:origin: SonarSource/sonarqube

/**
 * @see java.lang.ProcessBuilder#command(List)
 */
@Override
public ProcessBuilder command(List<String> commands) {
 builder.command(commands);
 return this;
}

代码示例来源:origin: SonarSource/sonarqube

/**
 * @see java.lang.ProcessBuilder#command()
 */
@Override
public List<String> command() {
 return builder.command();
}

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

/**
 * Changes the program and arguments of this process builder.
 *
 * @param command
 *            the new operating system program and its arguments.
 * @return this process builder instance.
 */
public ProcessBuilder command(String... command) {
  return command(new ArrayList<String>(Arrays.asList(command)));
}

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

public int run() throws IOException, InterruptedException {
    File workingDir = builder.directory() == null ? new File(".") : builder.directory();
    System.out.println(String.format("Trying to run command: %s from %s", Arrays.toString(builder.command().toArray()), workingDir.getAbsolutePath()));
    Process process = builder.start();
    int exitCode = process.waitFor();
    System.out.println("Finished command: " + Arrays.toString(builder.command().toArray()) + ". Exit code: " + exitCode);
    if (exitCode != 0) {
      if (failOnError) {
        throw new RuntimeException(String.format("Command exited with code %s. \n Exception: %s", exitCode, IOUtils.toString(process.getErrorStream())));
      } else {
        LOGGER.error("Command exited with code {}. \n Exception: {}", exitCode, IOUtils.toString(process.getErrorStream()));
      }
    }
    return exitCode;
  }
}

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

public static AutoClosableProcess runNonBlocking(String step, String... commands) throws IOException {
  LOG.info("Step Started: " + step);
  Process process = new ProcessBuilder()
    .command(commands)
    .inheritIO()
    .start();
  return new AutoClosableProcess(process);
}

代码示例来源:origin: skylot/jadx

if (os.contains("win")) {
  new ProcessBuilder()
      .command(new String[]{"rundll32", "url.dll,FileProtocolHandler", url})
      .start();
  return;
      .command(new String[]{"open", url})
      .start();
  return;
if (env.get("BROWSER") != null) {
  new ProcessBuilder()
      .command(new String[]{env.get("BROWSER"), url})
      .start();
  return;

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

public static void runBlocking(String step, Duration timeout, String... commands) throws IOException {
  LOG.info("Step started: " + step);
  Process process = new ProcessBuilder()
    .command(commands)
    .inheritIO()
    .start();
  try (AutoClosableProcess autoProcess = new AutoClosableProcess(process)) {
    final boolean success = process.waitFor(timeout.toMillis(), TimeUnit.MILLISECONDS);
    if (!success) {
      throw new TimeoutException();
    }
  } catch (TimeoutException | InterruptedException e) {
    throw new RuntimeException(step + " failed due to timeout.");
  }
  LOG.info("Step complete: " + step);
}

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

Process p = new ProcessBuilder().inheritIO().command("command1").start();

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

@Override
public Atop create(AtopTable table, ZonedDateTime date)
{
  checkArgument(date.getZone().getRules().equals(timeZone.getRules()), "Split date (%s) is not in the local timezone (%s)", date.getZone(), timeZone);
  ProcessBuilder processBuilder = new ProcessBuilder(executablePath);
  processBuilder.command().add("-P");
  processBuilder.command().add(table.getAtopLabel());
  processBuilder.command().add("-r");
  processBuilder.command().add(DATE_FORMATTER.format(date));
  Process process;
  try {
    process = processBuilder.start();
  }
  catch (IOException e) {
    throw new PrestoException(ATOP_CANNOT_START_PROCESS_ERROR, format("Cannot start %s", processBuilder.command()), e);
  }
  return new AtopProcess(process, readTimeout, executor);
}

代码示例来源: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: jenkinsci/jenkins

final Thread t2 = new StreamCopyThread(pb.command()+": stderr copier", proc.getErrorStream(), out);
t2.start();
return new Channel("locally launched channel on "+ pb.command(),
  Computer.threadPoolForRemoting, proc.getInputStream(), proc.getOutputStream(), out) {

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

/**
 * Add a command string supplier result to the list of command strings.  If the supplier returns {@code null} or
 * an empty string, no string is added at that time.  The supplier is evaluated every time a command is run.
 *
 * @param commandStringSupplier the string supplier to get the string from (must not be {@code null})
 * @return this builder
 */
public Builder addCommand(Supplier<String> commandStringSupplier) {
  checkNotNullParam("commandString", commandStringSupplier);
  builderProcessor = builderProcessor.andThen(pb -> {
    final String string = commandStringSupplier.get();
    if (string != null && ! string.isEmpty()) pb.command().add(string);
    return pb;
  });
  return this;
}

代码示例来源:origin: stanfordnlp/CoreNLP

public BZip2PipedOutputStream(String filename, OutputStream err) throws IOException {
 String bzip2 = System.getProperty("bzip2", "bzip2");
 String cmd = bzip2; // + " > " + filename;
 //log.info("getBZip2PipedOutputStream: Running command: "+cmd);
 ProcessBuilder pb = new ProcessBuilder();
 pb.command(cmd);
 this.process = pb.start();
 this.filename = filename;
 OutputStream outStream = new FileOutputStream(filename);
 errWriter = new PrintWriter(new BufferedWriter(new OutputStreamWriter(err)));
 outGobbler = new ByteStreamGobbler("Output stream gobbler: " + cmd + " " + filename,
     process.getInputStream(), outStream);
 errGobbler = new StreamGobbler(process.getErrorStream(), errWriter);
 outGobbler.start();
 errGobbler.start();
}

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

/**
 * Add a command string to the list of command strings.
 *
 * @param commandString the literal string to add (must not be {@code null})
 * @return this builder
 */
public Builder addCommand(String commandString) {
  checkNotNullParam("commandString", commandString);
  checkNotEmptyParam("commandString", commandString);
  builderProcessor = builderProcessor.andThen(pb -> {
    pb.command().add(commandString);
    return pb;
  });
  return this;
}

代码示例来源:origin: facebook/stetho

private void doKill(DumperContext dumpContext, Iterator<String> argsIter) throws DumpException {
 String signal = ArgsHelper.nextOptionalArg(argsIter, OPTION_KILL_DEFAULT);
 try {
  Process kill = new ProcessBuilder()
    .command("/system/bin/kill", "-" + signal, String.valueOf(android.os.Process.myPid()))
    .redirectErrorStream(true)
    .start();
  // Handle kill command output gracefully in the event that the signal delivered didn't
  // actually take out our process...
  try {
   InputStream in = kill.getInputStream();
   Util.copy(in, dumpContext.getStdout(), new byte[1024]);
  } finally {
   kill.destroy();
  }
 } catch (IOException e) {
  throw new DumpException("Failed to invoke kill: " + e);
 }
}

代码示例来源:origin: com.h2database/h2

private static int exec(String... args) {
  ByteArrayOutputStream buff = new ByteArrayOutputStream();
  try {
    ProcessBuilder builder = new ProcessBuilder();
    // The javac executable allows some of it's flags
    // to be smuggled in via environment variables.
    // But if it sees those flags, it will write out a message
    // to stderr, which messes up our parsing of the output.
    builder.environment().remove("JAVA_TOOL_OPTIONS");
    builder.command(args);
    Process p = builder.start();
    copyInThread(p.getInputStream(), buff);
    copyInThread(p.getErrorStream(), buff);
    p.waitFor();
    String output = new String(buff.toByteArray(), StandardCharsets.UTF_8);
    handleSyntaxError(output);
    return p.exitValue();
  } catch (Exception e) {
    throw DbException.convert(e);
  }
}

代码示例来源:origin: stanfordnlp/CoreNLP

/**
 * Start the process defined by the ProcessBuilder, and run until complete.
 * 
 * @param builder The ProcessBuilder defining the process to run.
 * @param output  Where the process output should be written. If null, the
 *                process output will be written to System.out.
 * @param error   Where the process error output should be written. If null,
 *                the process error output will written to System.err. 
 */
public static void run(ProcessBuilder builder, Writer output, Writer error) {
 try {
  Process process = builder.start();
  consume(process, output, error);
  int result = process.waitFor();
  if (result != 0) {
   String msg = "process %s exited with value %d";
   throw new ProcessException(String.format(msg, builder.command(), result));
  }
 } catch (InterruptedException | IOException e) {
  throw new ProcessException(e);
 }
}

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

/**
 * Add a command string provider to the list of command strings.  The provider can add multiple strings to
 * the consumer that is provided to it.  The provider must not provide {@code null} or empty strings.
 *
 * @param consumer the consumer which can provide the command strings to add (must not be {@code null})
 * @return this builder
 */
public Builder addCommand(Consumer<Consumer<String>> consumer) {
  checkNotNullParam("commandString", consumer);
  builderProcessor = builderProcessor.andThen(pb -> {
    consumer.accept(string -> pb.command().add(checkNotEmptyParam("string", checkNotNullParam("string", string))));
    return pb;
  });
  return this;
}

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

/**
 * Executes Hadoop command line tool.
 *
 * @param args Arguments for Hadoop command line tool.
 * @return Process exit code.
 * @throws Exception If failed.
 */
private int executeHadoopCmd(String... args) throws Exception {
  ProcessBuilder procBuilder = createProcessBuilder();
  List<String> cmd = new ArrayList<>();
  cmd.add(hadoopHome + "/bin/hadoop");
  cmd.addAll(Arrays.asList(args));
  procBuilder.command(cmd);
  log().info("Execute: " + procBuilder.command());
  return watchProcess(procBuilder.start());
}

相关文章