Groovy for Domain-specific Languages - Second Edition

(nextflipdebug2) #1
Chapter 2

[ 31 ]

The Groovy compiler – groovyc


Using the Groovy script engine, we can execute Groovy scripts from the command
line. For experimenting and debugging our scripts, we can run them interactively in
the Groovy shell or the Groovy console. To build our Groovy programs into larger
apps that require more than one class, or to integrate our Groovy programs into
existing Java applications, we need to be able to compile Groovy.


The groovyc command works exactly the same way as javac does. It takes a Groovy
source file and compiles it into a corresponding class file that can be run on the JVM.
Our script examples up to now have not defined a class. The Groovy compiler will
wrap our Groovy scripts into an executable class file, which can be invoked with the
java command as if they had a public static void main() method.


Let's take the JMX monitor.groovy script that we wrote earlier, and compile and
run it:


$ groovyc monitor.groovy


$ java -cp $GROOVY_HOME/embeddable/groovy-all-2.4.4.jar:. monitor


HEAP USAGE


Memory usage : 1118880


Memory usage after GC: 607128


The Groovy IDE and editor integration


If you are going to do any amount of serious Groovy coding, you will want to work
with Groovy in your favorite IDE.


NetBeans


Of the popular IDE environments, NetBeans was the first to provide built-in Groovy
support. From NetBeans 6.5 onwards, Groovy support is available from within any
of the Java bundles without any additional plugins being required. By default, you
have excellent Groovy source editing with syntax highlighting, source folding, and
code completion. You can mix and match Groovy with Java in your projects, or build
a full Groovy on Grails-based project from scratch. You can download the latest
NetBeans installation from https://netbeans.org/downloads/.

Free download pdf