Using Groovy Scripts from Java

If we want to use Groovy script as is in Java, we can use JSR 223.

Java Specification Request (JSR) 223 bridges the Java Virtual Machine (JVM) and scripting languages (see the Java Scripting Programmer’s Guide in Appendix 1, Web Resources). It provides a standard way to interact between Java and several languages with implementations of the JSR 223 scripting-engine API. We can download and use JSR 223 with Java 5. It is included by default in Java 6.

JSR 223 is an option more suited to other languages on the JVM than to Groovy. Groovy’s ability to jointly compile Java and Groovy lessens the need for something like JSR 223.

To call a (not-precompiled) script from Java, use the script engine. We can obtain ...

Get Programming Groovy 2 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.