Search code examples
javagroovygroovyshell

How to catch GroovyShell output?


I create a groovy engine with the GroovyShell class. Then I run a bunch of statements with the "evaluate" method.
Is there a way to catch the output of the engine so I can get "println" calls output?
Currently it goes to stdout although it is a swing application.


Solution

  • You can just assign your custom Writer (eg. StringWriter) to out property in the binding and pass it to the GroovyShell.

    def binding = new Binding();
    binding.setProperty("out", new YourWriter())
    new GroovyShell(binding);