virendra chaudhary
virendra chaudhary

Reputation: 179

How to launch groovy console during runtime of a java program

I want to launch groovy console during runtime of my java program and Groovy Console should be able to access some java variables
ex.

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

Now in groovy console x should be accessible

println x  
==> 5

Upvotes: 3

Views: 866

Answers (2)

virendra chaudhary
virendra chaudhary

Reputation: 179

I am just summarizing here how we can launch Groovy Console in run time.
We can include below code in java class or in 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
    }

}

Upvotes: 1

rdmueller
rdmueller

Reputation: 11042

Take a look at the /bin folder of your groovy installation. There you'll find the GroovyConsole.bat script in which you'll find a reference to groovy.ui.Console: http://docs.groovy-lang.org/latest/html/gapi/groovy/ui/Console.html

This contains the main() method of the console - you should be able to launch it though this method.

Regarding the binding of variables to the console, I guess the documentation link above will help you to figure out how to make your local variables accessible from within the console.

Upvotes: 3

Related Questions