我正在使用JSCH在linux上执行一些命令并获取该命令的输出并在我的JAVAFX应用程序中的活动日志上打印它。
这是一个问题:打印一些行后活动日志区域卡住了。但是,如果我从窗口切换并返回应用程序继续在日志区域上打印行。我已经调试了几次但是无法解决问题。
以下是代码
channel.connect();
PrintStream commander = new PrintStream(channel.getOutputStream(), true);
commander.println(command_cd);
commander.println(" exit;");
commander.close();
InputStream outputstream_from_the_channel = channel.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(outputstream_from_the_channel));
String jarOutput;
BufferedWriter bw = new BufferedWriter(new FileWriter(resultLogFile.getAbsolutePath(), true));
while ((jarOutput = reader.readLine()) != null) {
this.logger.info("Status Update = " + jarOutput);
System.out.print("Status Update on screen ="+jarOutput + "\n");
bw.write(jarOutput);
bw.newLine();
outputFromUnix.append(jarOutput).append("\n");
// Display in activity log area in realtime.
if (DeploymentTaskController.actLogTArea != null) {
System.out.println("outputFromUnix.toString()---->>>>> " + outputFromUnix.toString());
//actlogArea is TextArea
DeploymentTaskController.actLogTArea.setText(outputFromUnix.toString());
DeploymentTaskController.actLogTArea.end();
}
}
bw.close();
reader.close();
答案 0 :(得分:0)
JavaFX有一个名为“JavaFX Application Thread”的特殊线程来更新所有UI。此线程不应用于任何非UI操作,并且所有UI操作必须才能在该线程上运行。
确保在单独的线程中运行代码并将所有UI代码包装到Platform#runLater()
以执行
// Using separate thread to run data loading code
new Thread(new Runnable() {
public void run() {
channel.connect();
// [...] all your channel reading code
// Updating UI from JavaFX Application Thread
if (DeploymentTaskController.actLogTArea != null) {
System.out.println("outputFromUnix.toString()---->>>>> " + outputFromUnix.toString());
final String outputStr = outputFromUnix.toString();
Platform.runLater(new Runnable() {
@Override
public void run() {
DeploymentTaskController.actLogTArea.setText(outputStr);
DeploymentTaskController.actLogTArea.end();
}
});
}
bw.close();
reader.close();
}
}).start();
JavaFX中还有一个特殊的并发API用于此类任务,请参见此处:http://docs.oracle.com/javafx/2/threads/jfxpub-threads.htm