What I Did To Overcome The Most Exhaustive IT Project In My Life

The story was shared by Carrie Leder Nunemaker, a project manager at Arkansas Supreme Court, who has more than 8 years of experiences as an IT project manager. (Visit Carrie’s LinkedIn here). As a…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




10.9 Using Groovy Scripts from Java

Programming Groovy 2 — by Venkat Subramaniam (92 / 152)

👈 10.8 Using Groovy Scripts from Groovy | TOC | Part 3 MOPping Groovy 👉

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 it from ​ScriptEngineManager​ by calling the ​getEngineByName​ method. To execute our scripts from within our Java code, call its ​eval​ method. To use Groovy scripts, we need to make sure ​…/jsr223-engines/groovy/build/groovy-engine.jar​ is in our ​classpath​.

Let’s look at an example to execute a little Groovy script from within Java. (With Java comes the pleasure of handling exceptions we don’t care about. The rest of the examples in this chapter don’t show the exception-handling code, but remember to put it where needed.)

The output from the preceding code is as follows:

In this example, our Groovy script is embedded in the string parameter to the ​eval​ method. Unlike in this example, in reality the script may not be hard-coded. It may be in a file, an input stream, a dialog box, and so on. In that case we’ll find other overloaded versions of…

Add a comment

Related posts:

Thalassemia

In the midnight he was fighting with death to survive for his life but what can he do? he was helpless and his condition was miserable. His mother and father were praying to Allah for their son but…

Exercises from Chapter 11

Printed in full color.For this new edition of the best-selling Learn to Program, Chris Pine has taken a good thing and made it even better. First, he used the feedback from hundreds of reader e-mails to update the content and make it even clearer. Second, he updated the examples in the book to use the latest stable version of R

Kabab Digi Recipe

Kabab Digi is one of the conventional and delectable Iranian dishes, which is kebab however somewhat not the same as the ordinary grill ones since it simply must be on a skillet! Kabab tabei is…