JSR-223 is designed to allow Java access to many scripting languages. This means that it also provides hooks for Groovy to those other languages.

JSR-223 comes with Java 6. Here is how you can use it. First we define a script manager:

mgr = new javax.script.ScriptEngineManager()

Now, we can call out to JavaScript:

// included in Java 6
println 'javascript: ' + mgr.getEngineByName("javascript").eval('''
function factorial(n) {
    if (n == 0) {
        return 1;
    }
    return n * factorial(n - 1);
}
factorial(4)
''')

Or JRuby:

// requires jruby and jruby-engine jars
println 'jruby: ' + mgr.getEngineByName("jruby").eval('''
def factorial(n)
    if n == 0
        1
    else
        n * factorial(n - 1)
    end
end
factorial(4)
''')

Or Jython:

// requires jython and jython-engine jars
engine = mgr.getEngineByName("jython")
engine.eval('''
def factorial(n):
    i=fact=1
    while i <= n:
        fact=fact*i
        i=i+1
    return fact

result = factorial(4)
''')
println 'jython: ' + engine.result

Or Clojure:

// requires clojure and clojure-engine jars
// Note: doesn't seem officially supported, hacked a version from here:
// http://wiki.github.com/pmf/clojure-jsr223
engine = mgr.getEngineByName("clojure")
println 'clojure: ' + engine.eval('''
(defn factorial [n]
   (if (< n 2)
       1
       (* n (factorial (- n 1)))))
(factorial 4)
''')

Or Jaskell:

// requires jaskell and jaskell engine and jparsec and jfunutil jars
engine = mgr.getEngineByName("jaskell")
engine.eval('factorial n = if n > 0 then n * factorial (n-1) else 1')
println 'jaskell: ' + engine.eval('factorial 4')

The output from running these scripts is:

javascript: 24.0
jruby: 24
jython: 24
clojure: 24
jaskell: 24

See also: Accessing Groovy from Java via JSR-223