How to start groovy console while running java program

I want to run groovy console while my java program is running and groovy The console needs to be able to access some java variables
ex.

int x = 5 ;// a variable in java program   
launchGroovyConsole(); // hypothetical method to launch groovy console  

      

The console x should now be available in groovy

println x  
==> 5

      

+3


source to share


2 answers


I'm just summarizing here how we can start the Groovy console at runtime.
We can include below code in java class or Groovy class to launch Groovy console.



import groovy.ui.Console
public class TestGroovyConsole{
    public static void main(String[] args){
        int x = 5;
        Console console = new Console();
        console.setVariable("x",x);// to make x available in console
        console.run(); // to launch console
    }

}

      

+1


source


Take a look at /bin

your groovy installation folder . There you will find a GroovyConsole.bat

script where you will find a link to groovy.ui.Console

: http://docs.groovy-lang.org/latest/html/gapi/groovy/ui/Console.html

This contains the main()

console method - you can run it using this method.



As far as binding variables to the console, I think the documentation link above will help you understand how to make local variables available from the console.

+3


source







All Articles