【问题标题】:ProcessBuilder.inheritIO() sending output to the wrong placeProcessBuilder.inheritIO() 将输出发送到错误的地方
【发布时间】:2014-05-20 07:22:16
【问题描述】:

我正在使用inheritIO() 将我程序中子进程的输出重定向到System.outSystem.err,并将输入重定向到System.in

这些都是System.setOut()之类的重定向:

// Reassign System IO
System.setIn(cpanel.getConsole().getInputStream());
System.setOut(new PrintStream(cpanel.getConsole().getOutputStream()));
System.setErr(new PrintStream(cpanel.getConsole().getOutputStream()));

但是当我运行这个过程时:

String[] fullargs = new String[sargs.length+4];
fullargs[0] = "java";
fullargs[1] = "-classpath";   // Runtime classpath option.
fullargs[2] = cpath;          // Specify the classpath.
fullargs[3] = mname;          // Specify class to run.

for(int i=0; i<sargs.length; i++)
{
    fullargs[i+4] = sargs[i]; // Put together arguments.
}

ProcessBuilder proc = new ProcessBuilder()
                            .inheritIO()
                            .command(fullargs);


try
{
    System.out.println("RUNNING...");
    proc.start();
}
catch(IOException ioe)
{
    JOptionPane.showMessageDialog(null,
        "There was a system error invoking this program.",
        "ERROR",
        JOptionPane.ERROR_MESSAGE);
}

它重定向到过去 System.out 等,而不是它们被重定向到的位置。

如果我注释掉 inheritIO() 行,输出会随着时间的推移而丢失,并且不会出现在任何地方。使用inheritIO(),它会转到父进程的标准控制台,而不是重定向的控制台。我打印“RUNNING”的行转到正确的重定向位置。换句话说,inheritIO() 正在做它应该做的事情如果我没有重定向父进程的输出流。它将转到父进程的旧控制台。

我不知道为什么会发生这种情况,我正在把头发拉出来。我已经看到inheritIO() 在 Windows 中不起作用,但这个问题在 Mac OS 和 Linux 上是一样的。我正在使用 Java 7。

【问题讨论】:

  • 这个问题能给你一点线索吗? stackoverflow.com/questions/14165517/…
  • @fluminis 那里的解决方案适用于 Java 6 及更早版本。 Java 7 及更高版本中的inheritIO() 函数旨在自动执行此操作。

标签: java redirect java-7 processbuilder system.out


【解决方案1】:

请注意我的回答:https://stackoverflow.com/a/32350856/5226711

适用于您的问题,这意味着您可以使用 https://stackoverflow.com/a/14165567/5226711 中提出的 StreamGobbler 的改编版本:

private class StreamGobbler extends Thread {
    private InputStream in;
    private PrintStream out;

    private StreamGobbler(InputStream in, PrintStream out) {
        this.in = in;
        this.out = out;
    }

    @Override
    public void run() {
        try {
            BufferedReader input = new BufferedReader(new InputStreamReader(in));
            String line = null;
            while ((line = input.readLine()) != null)
                out.println(line);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

并像这样使用它:

String[] fullargs = new String[sargs.length+4];
fullargs[0] = "java";
fullargs[1] = "-classpath";   // Runtime classpath option.
fullargs[2] = cpath;          // Specify the classpath.
fullargs[3] = mname;          // Specify class to run.

for(int i=0; i<sargs.length; i++)
{
    fullargs[i+4] = sargs[i]; // Put together arguments.
}

ProcessBuilder pb = new ProcessBuilder().command(fullargs);

try
{
    System.out.println("RUNNING...");
    Process p = pb.start();
    StreamGobbler pOut = new StreamGobbler(p.getInputStream(), new PrintStream(cpanel.getConsole().getOutputStream()));
    StreamGobbler pErr = new StreamGobbler(p.getErrorStream(), new PrintStream(cpanel.getConsole().getOutputStream()));
    pOut.start();
    pErr.start();
}
catch(IOException ioe)
{
    JOptionPane.showMessageDialog(null,
        "There was a system error invoking this program.",
        "ERROR",
        JOptionPane.ERROR_MESSAGE);
}

我的示例中不包括重定向孩子的标准输入。

【讨论】:

    猜你喜欢
    • 2012-01-21
    • 2012-08-19
    • 1970-01-01
    • 2015-02-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-09
    • 1970-01-01
    相关资源
    最近更新 更多