gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 14:23:20 25 4
gpt4 key购买 nike

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com