我正在尝试使用swing文本区域来模拟命令提示符。我正在启动服务器并将流传递到文本区域。问题是,我无法在文本区域中看到控制台输出。怎么解决?一旦进程停止(即服务器停止),我就能看到控制台输出,直到那时,我无法在文本区域看到控制台输出。粘贴代码块。我希望在服务器启动并运行时同时看到控制台输出。请帮忙。感谢。
注意:"激活运行"是启动服务器的命令。
private void startServerButtonMouseClicked(java.awt.event.MouseEvent evt) {
String[] command = {"CMD", "/C", "activator run"};
ProcessBuilder processbuilder = new ProcessBuilder(command);
processbuilder.directory(new File("D:\\temp"));
try {
Process process = processbuilder.start();
InputStream is = process.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line;
while ((line = br.readLine()) != null) {
//Appending the output to the text area
cartServiceTextArea.append(line +"\n");
}
//Wait to get exit value
try {
int exitValue = process.waitFor();
System.out.println("\n\nExit Value is " + exitValue);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (IOException ex) {
Logger.getLogger(DevUtilsUI.class.getName()).log(Level.SEVERE, null, ex);
}
}
答案 0 :(得分:0)
您正在事件调度线程(EDT)中运行循环,更新GUI。直到您的方法返回,文本区域将不会更新。无论如何,这种方法是不正确的。
您需要做的是从单独的线程读取输出并从那里更新文本区域。你不应该阻止EDT。
package ant.test;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
public class TextAreaCommandOutput extends JFrame {
private static final long serialVersionUID = 1L;
private JButton executeServer;
private JTextArea textArea;
public TextAreaCommandOutput() {
super("Execute Command and Output to TextArea");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
textArea = new JTextArea(10, 80);
add(new JScrollPane(textArea));
executeServer = new JButton("Start Server");
executeServer.addActionListener(new ActionListener() {
@Override public void actionPerformed(ActionEvent e) {
executeServer.setEnabled(false);
new Thread(new Runnable() {
@Override public void run() {
startServerButtonMouseClicked();
}
}).start();
}
});
add(executeServer, BorderLayout.SOUTH);
pack();
}
private void startServerButtonMouseClicked() {
String[] command = { "ls", "-lR" };
ProcessBuilder processbuilder = new ProcessBuilder(command);
try {
Process process = processbuilder.start();
InputStream is = process.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line;
while ((line = br.readLine()) != null) {
// Appending the output to the text area
textArea.append(line + "\n");
}
// Wait to get exit value
try {
int exitValue = process.waitFor();
System.out.println("\n\nExit Value is " + exitValue);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (IOException ex) {
Logger.getLogger(TextAreaCommandOutput.class.getName()).log(Level.SEVERE, null, ex);
}
executeServer.setEnabled(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override public void run() {
TextAreaCommandOutput f = new TextAreaCommandOutput();
f.setVisible(true);
}
});
}
}