Skip to: Site menu | Main content

Groovy 

      Download | Documentation | Developers | Community

An agile dynamic language for the Java Platform

JSR 223 Scripting with Groovy Add comment to Wiki View in Wiki Edit Wiki page Printable Version

Java 6 includes built-in support for JSR 223: Scripting for the Java Platform API classes. This framework can be used to host Script Engines in Java Applications. Numerous Scripting engines are available.

Here is how you can use JSR 223 to talk to Groovy from Java:

// require(url:'https://scripting.dev.java.net', jar:'groovy-engine.jar')
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;

public class CalcMain {
    public static void main(String[] args) throws Exception {
        ScriptEngineManager factory = new ScriptEngineManager();
        ScriptEngine engine = factory.getEngineByName("groovy");

        // basic example
        System.out.println(engine.eval("(1..10).sum()"));

        // example showing scripting variables
        engine.put("first", "HELLO");
        engine.put("second", "world");
        System.out.println(engine.eval("first.toLowerCase() + second.toUpperCase()"));
    }
}

The output is:

55
helloWORLD

To make this example work, add groovy-engine.jar from https://scripting.dev.java.net/ to your CLASSPATH and run this program using Java 6.

This next example illustrates calling an invokable function:

// require(url:'https://scripting.dev.java.net', jar:'groovy-engine.jar')
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.Invocable;

public class FactMain {
    public static void main(String[] args) throws Exception {
        ScriptEngineManager factory = new ScriptEngineManager();
        ScriptEngine engine = factory.getEngineByName("groovy");
        String fact = "def factorial(n) { n == 1 ? 1 : n * factorial(n - 1) }";
        engine.eval(fact);
        Invocable inv = (Invocable) engine;
        Object[] params = { new Integer(5) };
        Object result = inv.invokeFunction("factorial", params);
        System.out.println(result);
    }
}

And when run, results in:

120

Groovy has many mechanisms for integration with Java, some of which provider richer options than available with JSR 223 (e.g. greater configurability and more security control). JSR 223 is recommended when you need to keep the choice of language used flexible and you don't require integration mechanisms not supported by JSR-223.

See also: JSR-223 access to other JVM languages