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 (though we can't imagine why ). Using BSF allows you to be more loosely coupled to your scripting language; however, native integration is more light weight and offers closer integration.
Evaluate scripts or expressions using the shell
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.
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:
If you have an interface you wish to use which you implement in the Groovy script you can use it as follows:
This works fine if the Groovy class implements the inferface MyInterface. myObject can from then on be used as every other Java object implementing MyInterface.
One thing to remember is that the parseClass will try to create an object from your String fileName. Another way to do the gcl.parseClass is:
Note that all of the error handling has been removed -- you won't be able to do this in a java class. I actually use the Interface invocation for Groovy inside of a Utility Class.
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:
This will print "Hello, world!".
Embedding a Groovy Console in a Java Application
An interactive Groovy interpreter can be embedded in a running application for experimentation and testing. For a tutorial on how to do, including example code, see the cookbook example Embedding a Groovy Console in a Java Server Application.
An example for the integration of Groovy as scripting language into an application can be found at Integrating Groovy in an application - a success story
You can use the groovy-all-x.y.z.jar from the GROOVY_HOME/embeddable directory of your Groovy installation, or find the according artifact from your Gradle / Maven / Ant+Ivy build. Please also have a look at the download section to see how to get Groovy.