Java Reference
In-Depth Information
Figure 3.2. Using the JSR 223 ScriptEngine to invoke a Groovy script. Java creates a
ScriptEngineManager , which then yields a ScriptEngine . After supplying parameters to the engine, its
eval method is invoked to execute a Groovy script.
The next listing demonstrates the API in action in a simple “Hello, World!” Groovy script.
Listing 3.2. Using the ScriptEngine to execute a simple Groovy script
I retrieve the Groovy script engine by calling the getEngineByName method. I then
use two different overloads of the eval method: one that takes a String argument and
one that takes an implementation of the java.io.Reader interface. In the first case,
the supplied string needs to be the actual scripting code. For the reader, though, I use a
FileReader wrapped around the “Hello, Groovy!” script. The output is what you would
expect in each case.
Supplying Parameters to a Groovy Script
What if the Groovy script took input parameters and returned data? In the Groovy scripting
world this is handled through a binding . When I discuss the GroovyShell in the next
 
Search WWH ::




Custom Search