I'm using Groovy Interpreter in Java class, I'm trying to define a method and call it, here is my code:
Binding binding = new Binding();
binding.setVariable("aa", 1);
binding.setVariable("bb", 2);
GroovyShell shell = new GroovyShell(binding);
shell.evaluate("int add(int a,int b){return (a+b)}");
int value =(int) shell.evaluate("add(aa,bb);");
System.out.println(value);
And I've got this error:
Exception in thread "main" groovy.lang.MissingMethodException: No signature of method: Script2.add() is applicable for argument types: (java.lang.Integer, java.lang.Integer) values: [1, 2] Possible solutions: any(), wait(), run(), run(), find(), wait(long, int)
Please tell me how to define a function inside groovy and why my code is not working.
Best regards,
Script2, I think the shell treats eachevaluatecall independently. - chrylis -cautiouslyoptimistic-shellevaluation, thank you - Mehdi Bouzidi