GroovyShell: Native Execution

I am trying to embed groovy in a large Java application.

At startup, the Java application should load some groovy utilities.

Then the application should run other scripts several times. It is also necessary to enter some code in the GUI and execute it at the request of the user.

The problem I am facing is this:

I load the launch script as follows:

GroovyShell gShell = new GroovyShell();
gShell.evaluate(new FileReader("scripts/autoload.groovy"));

Suppose my autoload.groovy contains:

def prnt(m) {
    println("From Groovy: " + m);
}

It works great. But when I want to run a user command using:

gShell.evaluate("prnt 66");

I get an error: groovy.lang.MissingMethodException: No signature of method: Script2.prnt() is applicable for argument types: (java.lang.Integer) values: [66]

How will my script user gain access to already loaded methods?

Note. I also tried "autoload.prnt 88" and still get the error.

+5
source share
1 answer

evaluate Script

def prnt(m) {
    println("From Groovy: " + m);
}

Script, autoload.groovy, "" script. , , GroovyShell, , Script . - :

prnt = { m ->
    println("From Groovy: " + m);
}

prnt, . ,

def prnt = { m ->

Closure prnt = { m ->

, def type ( script), .

+4

All Articles