java.lang.ProcessBuilder类的使用及代码示例

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

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

ProcessBuilder介绍

[英]Creates operating system processes. See Process for documentation and example usage.
[中]创建操作系统进程。有关文档和示例用法,请参阅流程。

代码示例

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

public Process exec(String[] cmdarray, String[] envp, File dir)
  throws IOException {
  return new ProcessBuilder(cmdarray)
    .environment(envp)
    .directory(dir)
    .start();
}

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

ProcessBuilder builder = new ProcessBuilder("/bin/bash");
builder.redirectErrorStream(true);
Process process = builder.start();

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

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

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

Process invoke(String[] command) throws IOException {
  ProcessBuilder processBuilder = new ProcessBuilder(command);
  return processBuilder.start();
}

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

Process p = null;
ProcessBuilder pb = new ProcessBuilder("do_foo.sh");
pb.directory("/home");
p = pb.start();

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

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

代码示例来源: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: stackoverflow.com

import java.io.IOException;

public class CLS {
  public static void main(String... arg) throws IOException, InterruptedException {
    new ProcessBuilder("cmd", "/c", "cls").inheritIO().start().waitFor();
  }
}

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

public Process run(List<String> cmd, List<String> removeEnv,
          Map<String, String> environmentVariables) throws IOException {
 LOG.info("run(cmd, removeEnv, environmentVariables)");
 LOG.info("Starting cmd: " + cmd);
 ProcessBuilder pb = new ProcessBuilder(cmd);
 for (String key : removeEnv) {
  if(pb.environment().containsKey(key)) {
   LOG.info("Removing env var: " + key + "=" + pb.environment().get(key));
  }
  pb.environment().remove(key);
 }
 pb.environment().putAll(environmentVariables);
 logDebugInfo("========Starting process with env:========", pb.environment());
 printContentsOfDir(".");
 return pb.start();
}
private static void logDebugInfo(String msg, Map<String, String> props) {

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

ProcessBuilder pb = new ProcessBuilder("command", "argument");
pb.directory(new File(<directory from where you want to run the command>));
pb.inheritIO();
Process p = pb.start();
p.waitFor();

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

public ProcessOutputStream(String[] cmd, Writer writer) throws IOException {
 this(new ProcessBuilder(cmd), writer, writer);
}

代码示例来源:origin: jenkinsci/jenkins

public Channel launchChannel(String[] cmd, OutputStream out, FilePath workDir, Map<String,String> envVars) throws IOException {
  printCommandLine(cmd, workDir);
  ProcessBuilder pb = new ProcessBuilder(cmd);
  pb.directory(toFile(workDir));
  if (envVars!=null) pb.environment().putAll(envVars);
  return launchChannel(out, pb);
}

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

@Test
public void testCommandWrapperCommand() {
 CommandWrapper commandWrapper = CommandWrapper.create();
 commandWrapper.setCommand(COMMAND);
 ProcessBuilder processBuilder = commandWrapper.getBuilder();
 if (BaseEmulatorHelper.isWindows()) {
  assertEquals(WIN_COMMAND, processBuilder.command());
 } else {
  assertEquals(UNIX_COMMAND, processBuilder.command());
 }
 assertNull(processBuilder.directory());
 assertFalse(processBuilder.redirectErrorStream());
 assertEquals(ProcessBuilder.Redirect.PIPE, processBuilder.redirectError());
}

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

/**
  * @see java.lang.ProcessBuilder#start()
  */
 @Override
 public Process start() throws IOException {
  return builder.start();
 }
}

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

pb.environment().putAll(cookie);
final Process proc = pb.start();
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: spotbugs/spotbugs

public static void main(String args[]) {
    ProcessBuilder pb = new ProcessBuilder();
    pb.redirectErrorStream();
  }
}

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

/**
 * Creates the process build with appropriate environment to run Hadoop CLI.
 *
 * @return Process builder.
 */
private ProcessBuilder createProcessBuilder() {
  String sep = ":";
  String ggClsPath = HadoopJobEx.class.getProtectionDomain().getCodeSource().getLocation().getPath() + sep +
    HadoopJobTracker.class.getProtectionDomain().getCodeSource().getLocation().getPath();
  ProcessBuilder res = new ProcessBuilder();
  res.environment().put("HADOOP_HOME", hadoopHome);
  res.environment().put("HADOOP_CLASSPATH", ggClsPath);
  res.environment().put("HADOOP_CONF_DIR", testWorkDir.getAbsolutePath());
  res.environment().put("HADOOP_OPTS", filteredJvmArgs());
  res.redirectErrorStream(true);
  return res;
}

相关文章