-3

My grails app has an external java library which I developed. I want my grails app to be able to display my java library system.out statements (console output). How do I go about doing that?

I would rather not have to write it out to a file then read it in grails. There must be a better way of doing this.

**I want grails to display these to a text box as the happen(In a browser while its running(so the user can see))


Afer trying several of my own ways I have to settle for this way which I will post here. Thanks @Shaunak for your help and understanding.

**How I solved it.

In my java library I created a global class which all the System.outs will be wrote to

writeSouts(String sout){     
    //store here in a global string and keep appending to it as needs be          
}

Then in grails constantly call the java library method. and using Ajax write it out to the browser window.

Hope this helps anyone in the future who encounters the same problem.

Also it is not for testing but an integral part of my web app so that the user of the web app can was the java library is doing in the background.

MKB
  • 7,587
  • 9
  • 45
  • 71
hat_to_the_back
  • 99
  • 1
  • 12
  • Have you thought about turning these `system.out` statements into log statements? – reto Dec 16 '14 at 14:30
  • Once I put them into log statements what do I do then. @reto – hat_to_the_back Dec 16 '14 at 14:31
  • okay, to be clear, you just want your grails to 'Display' these on console and not necessarily 'read and display' ? – Shaunak Dec 16 '14 at 14:32
  • I want grails to display these to a user in a browser @Shaunak – hat_to_the_back Dec 16 '14 at 14:32
  • I found this other very similar question by you, http://stackoverflow.com/questions/27359816/how-to-read-console-ouput so are you able to read the console in your grails yet? and just need help in showing it on a HTML page? or you still on reading console log from inside grails app? ... Also, you will have to give a little more details on how exactly do you invoke this Java Library.. do you invoke it from command line and then it prints the output? – Shaunak Dec 16 '14 at 14:37
  • 1
    And folks, please give a reason when down voting. I think its a perfectly valid question.. – Shaunak Dec 16 '14 at 14:37
  • A logging framework would give you ways to reroute this output. – reto Dec 16 '14 at 14:41
  • 1
    i think, that the downvoters don't disagree with the question itself, but with the approach in general and the fact, that there is no code, what OP might already have tried. also, that there is a very similar question with at least one upvoted answer, has a smell. – cfrick Dec 16 '14 at 15:45
  • 1
    People are very quick to downvote a question because they dont read it properly or automatically assume that because it doesnt have sample code it hasnt been attempted. I think the premise of the question is quite clear, Im not asking anyone to code my method for me just simple a point in the right direction. Because of these quick assumptions, I havent been given the opportunity for people to help answer the question. SO afer trying several of my own ways I have to settle for this way which I will post here. Thanks @Shaunak for your help and understanding, – hat_to_the_back Dec 17 '14 at 10:55
  • then maybe you could elaborate, what the actual difference is between the two questions? you have a library in a jar you are using, this lib prints to stdout and you want to capture this and show it via grails in the browser. in both questions are answers, that do exactly this (which are of course not copy-and-paste-able, because the question is rather vague/broad) and in the comments are suggestions, how changing your lib would make this easier. so your "answer to self" now boils down to: "i changed the library!" – cfrick Dec 17 '14 at 18:19

1 Answers1

1

Without much details on how exactly you are invoking the Java Library, I will assume here that it is just some code that writes to console, and you want to invoke and read it from inside Grails controller and send it back to HTML. Here's what I would suggest. Instead of including the Java Code as a library, create a command line utility out of it.

So your java program will be invoked for testing like so,

java MyLib -arg1 -arg2

This will should out put something like this on the command line:

> This is my nice output

Now to to invoke and read this from grails, you can use Groovy's External Process Management and do something like this to invoke and read the output:

def process = "java MyLib -arg1 -arg2".execute()
def response = "Found text ${process.text}"

Your response var should now have value

Found Text This is my nice output 

You can now use this to do what ever in a grails controller, like send it back to HTML..

Shaunak
  • 17,377
  • 5
  • 53
  • 84