【发布时间】:2018-06-19 23:24:57
【问题描述】:
我在本地机器上有一个用 java 编写的程序。此程序使用 JSch 连接到远程计算机,并在用户界面中的 MessageConsole 上显示输出。
我可以在 UI 中更新MessageConsole,即使我启动的脚本没有终止并输出随机数。我可以阅读OutputStream 并将其显示在MessageConsole 上。它工作得很好。
我的问题是启动 C 程序后的输出。 OutputStream 上没有显示任何内容。
仅当程序完全终止时才会出现输出。
所有输出都使用
printf
或
std::cout
如果我手动登录远程机器并手动启动程序,我的终端会立即显示所有输出。
我使用ChannelExec 在远程机器上启动程序:
String program = "sudo ./foo/bar.out";
String continousOutput = "sh ~/foo/bar.sh";
//ProcessWorker(String command, SSHSession ssh, GUI gui)
pw = new ProcessWorker(program, ssh, this);
pw.execute();
ProcessWorker pw 是一个 SwingWorker 类,用于在后台保持连接打开并更新 GUI 而不会锁定它。
这段代码完成了所有的脏活。 SSH 连接已经建立并且可以正常工作。
protected Void doInBackground() throws Exception {
try {
exec = ssh.session.openChannel("exec");
//command is the String program or continousOutput
((ChannelExec)exec).setCommand(command);
exec.setInputStream(null);
((ChannelExec)exec).setErrStream(System.err);
InputStream in = exec.getInputStream();
exec.connect();
byte [] tmp = new byte[1024];
while(true) {
while(in.available()>0) {
int i = in.read(tmp, 0, 1024);
if(i<0)break;
String response = new String(tmp, 0, i);
check(response); //parse response
System.out.println(response);
//this.publish(); //desperate try to get something
}
if(exec.isClosed()) {
if(in.available() > 0) continue;
System.out.println("exit-status: " + exec.getExitStatus());
break;
}
try {Thread.sleep(1000);} catch(Exception ee) {}
}
exec.disconnect();
} catch (JSchException | IOException e) {
e.printStackTrace();
}
return null;
}
我不知道问题出在哪里。我的意思是它与脚本完美配合。
有什么想法吗?
【问题讨论】: