我正在尝试在我自己的控制台窗口中运行 plink。我从使用 Process.exec() 开始,效果很好。我开始使用 ProcessBuilder,现在在我终止进程之前不会发送输出。
我的代码如下所示:
class ConsoleOutputThread extends Thread {
public void start(String processName) {
// this was old code: Runtime r = Runtime.getRuntime();
try {
builder = new ProcessBuilder("plink", "-ssh", "192.168.3.21");
builder.redirectErrorStream(true);
process = builder.start();
//this was old code: process = r.exec (processName);
} catch (IOException ex) {
}
start();
}
@Override
public void run() {
try {
BufferedReader is = new BufferedReader(new InputStreamReader(process.getInputStream()));
writer = new BufferedWriter(new OutputStreamWriter(process.getOutputStream()));
try {
process.waitFor();
} catch (InterruptedException ex) {
}
char b[];
b = new char[1];
while(is.read(b, 0, 1)> 0) {
// this is for debug, normally sent to console
System.out.println("Got character: " + b[0]);
}
} catch (IOException ex) {
}
}
}
因此,当使用 Runtime.exec() 时一切正常。现在,使用 ProcessBuilder,读取功能将永远阻塞(实际上直到我终止进程,当一切都被吐出时)。但是,错误流有效,即如果我输入错误选项,我会在控制台中收到消息。我可能在这里遗漏了一些东西并寻求帮助。谢谢