I'm trying to extract a closure from a groovy script. I define the closure as
def printMe = {str ->println str}
in my groovy file, and then try to use it by grabbing it from the binding as follows:
GroovyScriptEngine gse = new GroovyScriptEngine(new String[] { "scripts" });
Binding binding = new Binding();
gse.run("test.groovy", binding);
Closure cls = (Closure) binding.getVariable("printMe");
cls.call("foo");
But I get the following error when I run this.
groovy.lang.MissingPropertyException: No such property:
printMe for class: groovy.lang.Binding
at groovy.lang.Binding.getVariable(Binding.java:55)
at GroovyTry.main(GroovyTry.java:19)
Is there a way to grab a closure (or a plain method) from a groovy script?