Java Reference
In-Depth Information
Listing 3.3. A JUnit test case to check the JSR 223 script engine results
The result is the same as running the Groovy script by itself using Groovy. Setting the val-
uesoftheinputvariablesistrivial.Theoutputvariablesneedtobecasttothe String type
and then converted to double s, but again the process is straightforward. If your goal is to
execute an external Groovy script from Java without introducing any Groovy dependencies
at all (other than adding the groovy-all JAR to your classpath), this mechanism works just
fine.
In the next section I want to relax that requirement. If you're willing to use some classes
from the Groovy standard library, life gets simpler.
3.2.2. Working with the Groovy Eval class
There are two special classes in the Groovy library, groovy.util.Eval and groovy
.lang.GroovyShell , specifically designed for executing scripts. In this section I'll
show examples using the Eval class, and in the next section I'll show GroovyShell . In
each case, the goal is still to invoke external Groovy scripts from Java.
The Eval class is a utility class (all its methods are static) for executing operations that
take none, one, two, or three parameters. The relevant methods are shown in table 3.1 .
Search WWH ::




Custom Search