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

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

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

ProcessBuilder.redirectErrorStream介绍

[英]Indicates whether the standard error should be redirected to standard output. If redirected, the Process#getErrorStream() will always return end of stream and standard error is written to Process#getInputStream().
[中]指示是否应将标准错误重定向到标准输出。如果重定向,进程#getErrorStream()将始终返回流的末尾,标准错误将写入进程#getInputStream()。

代码示例

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

private static ProcessBuilder stderr(ProcessBuilder pb, boolean redirectError) {
  if(redirectError)    pb.redirectErrorStream(true);
  return pb;
}

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

/**
 * @see java.lang.ProcessBuilder#redirectErrorStream(boolean)
 */
@Override
public ProcessBuilder redirectErrorStream(boolean b) {
 builder.redirectErrorStream(b);
 return this;
}

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

private int exec(String cmd) throws InterruptedException, IOException {
  ProcessBuilder pb = new ProcessBuilder(exe, cmd);
  pb.redirectErrorStream(true);
  Process p = pb.start();
  p.getOutputStream().close();
  ByteArrayOutputStream baos = new ByteArrayOutputStream();
  copy(p.getInputStream(), baos);
  int r = p.waitFor();
  if (r!=0)
    LOGGER.info(exe+" cmd: output:\n"+baos);
  return r;
}

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

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

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

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

代码示例来源:origin: alibaba/jstorm

protected static java.lang.Process launchProcess(final List<String> cmdlist,
                         final Map<String, String> environment) throws IOException {
  ProcessBuilder builder = new ProcessBuilder(cmdlist);
  builder.redirectErrorStream(true);
  Map<String, String> process_evn = builder.environment();
  for (Entry<String, String> entry : environment.entrySet()) {
    process_evn.put(entry.getKey(), entry.getValue());
  }
  return builder.start();
}

代码示例来源:origin: alibaba/jstorm

protected static Process launchProcess(final List<String> cmdlist,
                    final Map<String, String> environment) throws IOException {
  ProcessBuilder builder = new ProcessBuilder(cmdlist);
  builder.redirectErrorStream(true);
  Map<String, String> process_evn = builder.environment();
  for (Entry<String, String> entry : environment.entrySet()) {
    process_evn.put(entry.getKey(), entry.getValue());
  }
  return builder.start();
}

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

private static Process sendCommand(final List<String> commands) throws IOException
{
  return new ProcessBuilder(commands.toArray(new String[commands.size()]))
    .redirectErrorStream(true)
    .start();
}

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

private String getJavaCompilerVersion() {
  ProcessBuilder pb = new ProcessBuilder("javac", "-version");
  pb.redirectErrorStream(true);
  BufferedReader in = null;
  try {
    Process process = pb.start();
    in = new BufferedReader(new InputStreamReader(process.getInputStream()));
    String buffer = in.readLine();
    process.waitFor();
    return buffer;
  } catch (Exception e) {
    // ignored
  } finally {
    if (in != null) {
      try {
        in.close();
      } catch (IOException e) {
        // cannot help this
      }
    }
  }
  return "unknown";
}

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

private static boolean startProcess (String[] commands, File directory, final CharCallback callback) {
    try {
      final Process process = new ProcessBuilder(commands).redirectErrorStream(true).directory(directory).start();

      Thread t = new Thread(new Runnable() {
        @Override
        public void run () {
          BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()), 1);
          try {
            int c = 0;
            while ((c = reader.read()) != -1) {
              callback.character((char)c);						
            }
          } catch (IOException e) {
//                        e.printStackTrace();
          }
        }
      });
      t.setDaemon(true);
      t.start();
      process.waitFor();
      t.interrupt();
      return process.exitValue() == 0;
    } catch (Exception e) {
      e.printStackTrace();
      return false;
    }
  }
}

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

ProcessBuilder pb = new ProcessBuilder(
  "C:/Program Files/WinRAR/winrar",
  "x",
  "myjar.jar",
  "*.*",
  "new");
pb.directory(new File("H:/"));
pb. redirectErrorStream(true);

Process p = pb.start();

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

ProcessBuilder pb=new ProcessBuilder(command);
pb.redirectErrorStream(true);
Process process=pb.start();
BufferedReader inStreamReader = new BufferedReader(
  new InputStreamReader(process.getInputStream())); 

while(inStreamReader.readLine() != null){
  //do something with commandline output.
}

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

private String getGitConfig(final String configName, final String defaultValue) {
  try {
    final Process process = new ProcessBuilder("git", "config", configName).redirectErrorStream(true).start();
    return CharStreams.toString(new InputStreamReader(process.getInputStream(), StandardCharsets.UTF_8)).trim();
  } catch (Throwable ex) {
    return "YOUR_NAME";
  }
}

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

ProcessBuilder pb = new ProcessBuilder("tasklist");
pb.redirectErrorStream(true);
Process process = pb.start();
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line;
while ((line = reader.readLine()) != null)
  System.out.println("tasklist: " + line);
process.waitFor();

代码示例来源:origin: Tencent/tinker

public static void exec(ArrayList<String> args, File path) throws RuntimeException, IOException, InterruptedException {
  ProcessBuilder ps = new ProcessBuilder(args);
  ps.redirectErrorStream(true);
  if (path != null) {
    ps.directory(path);
  }
  Process pr = ps.start();
  BufferedReader ins = null;
  try {
    ins = new BufferedReader(new InputStreamReader(pr.getInputStream()));
    String line;
    while ((line = ins.readLine()) != null) {
      System.out.println(line);
    }
    if (pr.waitFor() != 0) {
      throw new RuntimeException("exec cmd failed! args: " + args);
    }
  } finally {
    try {
      pr.destroy();
    } catch (Throwable ignored) {
      // Ignored.
    }
    StreamUtil.closeQuietly(ins);
  }
}

代码示例来源:origin: sarxos/webcam-capture

public void startProcess() throws IOException {
  ProcessBuilder builder = new ProcessBuilder(buildCommand());
  builder.redirectErrorStream(true); // so we can ignore the error stream
  process = builder.start();
}

代码示例来源:origin: iBotPeaches/Apktool

public static String execAndReturn(String[] cmd) {
  ExecutorService executor = Executors.newCachedThreadPool();
  try {
    ProcessBuilder builder = new ProcessBuilder(cmd);
    builder.redirectErrorStream(true);
    Process process = builder.start();
    StreamCollector collector = new StreamCollector(process.getInputStream());
    executor.execute(collector);
    process.waitFor();
    if (! executor.awaitTermination(15, TimeUnit.SECONDS)) {
      executor.shutdownNow();
      if (! executor.awaitTermination(5, TimeUnit.SECONDS)) {
        System.err.println("Stream collector did not terminate.");
      }
    }
    return collector.get();
  } catch (IOException | InterruptedException e) {
    return null;
  }
}

代码示例来源:origin: KronicDeth/intellij-elixir

@NotNull
protected Process startProcess(@NotNull List<String> commands) throws IOException {
  ProcessBuilder builder = new ProcessBuilder(commands);
  setupEnvironment(builder.environment());
  builder.directory(myWorkDirectory);
  builder.redirectErrorStream(false);
  return builder.start();
}

代码示例来源:origin: Tencent/tinker

pb.redirectErrorStream(true);
Process pro = null;
LineNumberReader reader = null;

代码示例来源: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);
 }
}

相关文章