Using the Grails framework how would one go about reading console output. What I mean is I have a java library in my grails app that outputs(system.out.println(\"x\")) to the co
You can redirect the output with the System.setOut() method. Here is a quick example that redirects System.out to a text pane after five seconds:
import javax.swing.*;
import java.awt.*;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;
import java.util.Date;
public class TextCapture {
public static void main(String[] args) throws Exception {
JFrame jf = new JFrame();
jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
final JTextArea jta = new JTextArea();
jta.setPreferredSize(new Dimension(400, 200));
jf.getContentPane().add(jta);
jf.pack();
jf.setVisible(true);
OutputStream textOS = new OutputStream() {
@Override
public void write(int b) throws IOException {
jta.setText(jta.getText() + ((char)b));
}
};
for(int i = 0; i < 20; i++) {
Thread.sleep(1000);
if(i == 5) {
System.setOut(new PrintStream(textOS));
}
System.out.println(String.valueOf(new Date()));
}
}
}