java - ProcessBuilder.inheritIO() 将输出发送到错误的位置

标签 java redirect java-7 processbuilder system.out

我正在使用 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。

最佳答案

请注意我的回答: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);
}

我的示例中不包括重定向子级的 stdin。

关于java - ProcessBuilder.inheritIO() 将输出发送到错误的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23753359/

相关文章:

redirect - 使用不同主机名的带有斜杠的 nginx 别名重定向

java - 如何在不使用科学记数法的情况下将字符串解析为 double

java - 烦恼 - 如何禁用 Eclipse 类路径条目警告

java - 解析 Set-Cookie header 的 firefox 源代码的位置?

php - 输出用户消息后重定向 PHP 页面

java - 在jdk1.7中无法编译运行HelloWorld

java - 无效的目标版本 : 1. 7

java - EclipseLink 2.3 加 Oracle 11g 批量写入

java - 将图像文件添加到 JPanel 作为背景?

apache - 使用 .htaccess 匹配重写规则中的问号