Java Reference
In-Depth Information
Chapter 3. Code-level integration
This chapter covers
• Calling Groovy scripts from Java using JSR 223
• Calling Groovy scripts from Java using Groovy library classes
In
chapter 1
I reviewed many of Java's arguable weaknesses and drawbacks and suggested
ways that Groovy might help ameliorate them. Because that chapter was intended to be in-
troductory, I only suggested how Groovy can help, without showing a lot of code examples.
This chapter begins an examination of Java and Groovy integration in detail. In this chapter
I'll start using Groovy and Java together in fundamental ways, without worrying about
frameworks or addressing any particular use case. A guide to the techniques discussed in
this chapter is shown in
figure 3.1
.
Figure 3.1. Guide to integration features. Groovy can be accessed with Java classes alone using the JSR 223 script
engine. If you are willing to add some Groovy library classes to Java, the
Eval, GroovyShell
, and
Binding
classes make working with scripts easy. The best way to combine Groovy and Java is using classes for both
languages.