0

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 console and I want to display this in a textbox in my grails app. How do I go about doing this.

I dont want to have to write the messages out to a file and read them in that way or to a database. Is there someway of directly reading from the console.

Thanks in advance.

EDIT**

Its a third party jar file that I am using

hat_to_the_back
  • 99
  • 1
  • 12
  • 1
    Could you not skip the console and direct it to the textbox directly? – James Fox Dec 08 '14 at 14:12
  • @JamesFox its coming from my external java library which I am using in the grails app to complete certain tasks. How could i do that? :) – hat_to_the_back Dec 08 '14 at 14:13
  • instead of system.out, do something else with 'x' textbox.setText('x'), or whatever. dont know what grails is, nbut should be easy like this if its just a java lib – Bart Hofma Dec 08 '14 at 14:15
  • Grails is used for web apps, you cant do that unfortunately. Java library has no control over that. – hat_to_the_back Dec 08 '14 at 14:16

2 Answers2

1

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()));
        }
    }

}
BarrySW19
  • 3,759
  • 12
  • 26
  • Thats redirecting it to a text pane in java. Im tyring to redirect it in a grails applicaztion :) – hat_to_the_back Dec 08 '14 at 14:52
  • 1
    Oh, you talk about a text box in the question - but the principal applies whatever you want to do with the text - just have the OutputStream send it wherever you want it. – BarrySW19 Dec 08 '14 at 15:39
-1

We can use log4j configuration, write your app logs in a file, keep reading same file with your app. This should suffice your requirement. Read more on http://grails.org/doc/latest/guide/conf.html section 5.1.2 Logging

Tushar
  • 215
  • 1
  • 2
  • 9