java – 进程输出仅在进程完成后才可用
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java – 进程输出仅在进程完成后才可用,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2889字,纯文字阅读大概需要5分钟。
内容图文
![java – 进程输出仅在进程完成后才可用](/upload/InfoBanner/zyjiaocheng/951/2dc0e274b7694cb7b347ef092c284ded.jpg)
我有一个Runnable从外部调用的exe读取Console输出(见下文)并将其写入日志文件和JTextArea.
但是我的Runnable在exe完全完成之前不会在JTextArea中显示控制台输出.如何实现打印控制台输出?
简明扼要的代码示例如下:
//主要
import java.awt.*;
import java.io.IOException;
import javax.swing.*;
public class Example extends JFrame {
private static final long serialVersionUID = 1L;
public static int maxX, maxY;
public static JTextArea ta = new JTextArea(20, 60);//For LOG display window
public static void main(String args[] ) throws IOException
{
new Example();
}
public Example() {
this.setTitle("Example");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//MAIN Panel
final JPanel main = new JPanel();
JButton RunButton = button.run(main);
main.add(RunButton);
Container container = getContentPane();
container.add(main);
this.pack();
this.setVisible(true);
}
}
//按钮动作侦听器
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.*;
public class button {
public static JButton run( final JPanel parent ) {
JButton RunButton = new JButton();
RunButton.setText("Start!");
RunButton.addActionListener(
new ActionListener()
{
public void actionPerformed( ActionEvent event)
{
try
{
//Set up LOG Display
JDialog dialog = new JDialog((JFrame)null, "Working...");
JPanel temp_panel = new JPanel();
temp_panel.add(new JScrollPane(Example.ta));
dialog.getContentPane().add(temp_panel);
dialog.pack();
dialog.setVisible(true);
//Build the Command
ArrayList<String> command = new ArrayList<String>();
command.add("ping");
command.add("127.0.0.1");
//Start the process
Process p = new ProcessBuilder(command).start();
//Starts LOG display capture in separate thread
SwingUtilities.invokeLater(new execute(p));
//Wait for call to complete
p.waitFor();
}
catch(Exception err)
{
JOptionPane.showMessageDialog( parent, "Error Executing Run!", "Warning", JOptionPane.ERROR_MESSAGE );
}
}//end ActionPerformed
});
return RunButton;
}
}
// Runnable接口
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class execute implements Runnable {
String line;
Process p;
public execute ( Process process ) {
p = process;
}
public void run() {
try {
//Read Process Stream Output and write to LOG file
BufferedReader is = new BufferedReader( new InputStreamReader(p.getInputStream()));
while ( (line = is.readLine()) != null ) {
Example.ta.append(line + "\n");
}
System.out.flush();
} catch(Exception ex) { ex.printStackTrace(); }
}
}
解决方法:
也许是因为你不尊重Swing’s threading policy.所有对swing组件的访问必须在事件派发线程中完成.因此,您的runnable应该使用SwingUtilities.invokeLater来更新EDT中的文本区域,而不是在单独的线程中.
编辑:正如他在评论中提到的那样:JTextArea.append是线程安全的,所以这里并不是绝对需要的.不过,我仍然会这样做,因为如果文本区域的附加被任何其他Swing交互替换或补充,那么它将不再是线程安全的.
也可能是外部进程没有发送任何换行符,这使得readLine阻塞直到找到一个或者到达通信结束.
内容总结
以上是互联网集市为您收集整理的java – 进程输出仅在进程完成后才可用全部内容,希望文章能够帮你解决java – 进程输出仅在进程完成后才可用所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。