My new website on Artificial Intelligence and Machine Learning www.aimlfront.com

How to integrate Java with Groovy

Groovy is a great language just on its own in various scenarios. It is also extremely useful in mixed Groovy/Java environments. With this in mind, Groovy has been designed to be very lightweight and easy to embed into any Java application system.
There are three main approaches for natively integrating Groovy with Java. Each of these is discussed in more detail below.
Alternatively, you can use the Bean Scripting Framework to embed any scripting language into your Java code, giving you other language options if you needed them . Using BSF allows you to be more loosely coupled to your scripting language; however, native integration is more light weight and offers closer integration.
Assuming you have a file called test.groovy, which contains (as in your example):
 def hello_world() {
   println "Hello, world!"
 }

Evaluate scripts or expressions using the shell

You can evaluate any expression or script in Groovy using the GroovyShell.
The GroovyShell allows you to pass in and out variables via the Binding object.
 new GroovyShell().parse(new File("src/main/resources/test.groovy")).invokeMethod("hello_world", null);

Evaluate Scripts with a Common Base Class

It is often useful to make your groovy scripts extend a base class of your choosing so that the script can access common methods. This can be achieved by setting the script base class property on the compile configuration and passing this new compiler configuration to the shell.
 new CompilerConfiguration().setScriptBaseClass("ScriptBaseTestScript");

 abstract class ScriptBaseTestScript extends Script {
  def foo() {
	"this is foo"
  }
 }

Dynamically loading and running Groovy code inside Java

You can use the GroovyClassLoader to load classes dynamically into a Java program and execute them (or use them) directly. The following Java code shows an example:
 Class scriptClass = new GroovyClassLoader().parseClass( new File("src/main/resources/test.groovy")) ;
 Object scriptInstance = scriptClass.newInstance() ;
 scriptClass.getDeclaredMethod("hello_world", new Class[] {}).invoke( scriptInstance, new Object[] {});

Otherthan above three aproaches, we have one more approach to make use of Groovy with Java.

If you are clearly focused to use Groovy the GroovyScriptEngine is the most complete solution.

The GroovyScriptEngine

The most complete solution for people who want to embed groovy scripts into their servers and have them reloaded on modification is the GroovyScriptEngine. You initialize the GroovyScriptEngine with a set of CLASSPATH like roots that can be URLs or directory names. You can then execute any Groovy script within those roots. The GSE will also track dependencies between scripts so that if any dependent script is modified the whole tree will be recompiled and reloaded.
Additionally, each time you run a script you can pass in a Binding that contains properties that the script can access. Any properties set in the script will also be available in that binding after the script has run. Here is a simple example:
 Class scriptClass = new GroovyScriptEngine( "." ).loadScriptByName( "src/main/resources/test.groovy");
 Object scriptInstance = scriptClass.newInstance() ;
 scriptClass.getDeclaredMethod( "hello_world", new Class[] {}).invoke( scriptInstance, new Object[] {});