在 Java 6 中使用继承的 stdin/stdout/stderr 启动进程

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/60302/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 10:54:11  来源:igfitidea点击:

Starting a process with inherited stdin/stdout/stderr in Java 6

javaioprocesspipe

提问by John Calsbeek

If I start a process via Java's ProcessBuilderclass, I have full access to that process's standard in, standard out, and standard error streams as Java InputStreamsand OutputStreams. However, I can't find a way to seamlessly connect those streams to System.in, System.out, and System.err.

如果我通过 Java 的ProcessBuilder类启动一个进程,我可以完全访问该进程的标准输入、标准输出和标准错误流,如 JavaInputStreamsOutputStreams. 不过,我不能找到一种方法,这些流无缝连接System.inSystem.outSystem.err

It's possible to use redirectErrorStream()to get a single InputStreamthat contains the subprocess's standard out and standard error, and just loop through that and send it through my standard out—but I can't find a way to do that and let the user type into the process, as he or she could if I used the C system()call.

可以使用redirectErrorStream()获取一个InputStream包含子流程的标准输出和标准错误的单曲,然后循环遍历并通过我的标准输出发送它——但我找不到一种方法来做到这一点,让用户输入到流程中,如果我使用 Csystem()调用,他或她就可以做到。

This appears to be possible in Java SE 7 when it comes out—I'm just wondering if there's a workaround now. Bonus points if the result of isatty()in the child process carries through the redirection.

这在 Java SE 7 出现时似乎是可能的——我只是想知道现在是否有解决方法。如果isatty()子进程中的结果通过重定向,则加分。

采纳答案by John Meagher

You will need to copy the Processout, err, and input streams to the System versions. The easiest way to do that is using the IOUtilsclass from the Commons IO package. The copy methodlooks to be what you need. The copy method invocations will need to be in separate threads.

您需要将Processout、err 和 input 流复制到系统版本。最简单的方法是使用Commons IO 包中的IOUtils类。该复制方法看起来是你需要的。复制方法调用将需要在单独的线程中。

Here is the basic code:

下面是基本代码:

// Assume you already have a processBuilder all configured and ready to go
final Process process = processBuilder.start();
new Thread(new Runnable() {public void run() {
  IOUtils.copy(process.getOutputStream(), System.out);
} } ).start();
new Thread(new Runnable() {public void run() {
  IOUtils.copy(process.getErrorStream(), System.err);
} } ).start();
new Thread(new Runnable() {public void run() {
  IOUtils.copy(System.in, process.getInputStream());
} } ).start();

回答by Eelco

A variation on John's answer that compiles and doesn't require you to use Commons IO:

John 的答案的一个变体,它编译并且不需要您使用 Commons IO:

private static void pipeOutput(Process process) {
    pipe(process.getErrorStream(), System.err);
    pipe(process.getInputStream(), System.out);
}

private static void pipe(final InputStream src, final PrintStream dest) {
    new Thread(new Runnable() {
        public void run() {
            try {
                byte[] buffer = new byte[1024];
                for (int n = 0; n != -1; n = src.read(buffer)) {
                    dest.write(buffer, 0, n);
                }
            } catch (IOException e) { // just exit
            }
        }
    }).start();
}

回答by claude

For System.inuse the following pipein()instead of pipe()

对于System.in使用以下pipein()代替pipe()

pipein(System.in, p.getOutputStream());

Implementation:

执行:

private static void pipein(final InputStream src, final OutputStream dest) {

    new Thread(new Runnable() {
        public void run() {
            try {
               int ret = -1;
               while ((ret = System.in.read()) != -1) {
                  dest.write(ret);
                  dest.flush();
               }
            } catch (IOException e) { // just exit
            }
        }
    }).start();

}