Groovy is a relatively new programming language. I like to think of it as Java on steroids. It is based on Java, so all of Java code is automatically Groovy code, but not vice versa. Groovy is also compiled to Java byte-code, so it can run on every JRE. Better yet, you can use GDK (Groovy Development Kit) which provides many very easy to use APIs – working with files, XML is lot easier than in Java.
Some of its main characteristics are:
- it is real object oriented language,
- it is a dynamic language,
- it can be used for scripting.
This last thing means that you can write Groovy script file and execute it from the command line without the need to compile it first, and that’s what makes it appealing. You get to use real powerful language within simple script. Of course, lots of things you’d write in Groovy could be done using shell scripts and if you’re not a programming soul, you won’t find many usefulness in it. Nevertheless, some of my next posts will include ready-to-use Groovy scripts that you may find useful, so here are some hints on what you need to do in order to run Groovy on your PC or server.
First of all, you’ll need JRE (Java Runtime Environment), which is probably already installed on every desktop PC and most of servers. As you’re probably going to run scripts from shell or command line, ensure that you have JAVA_HOME environment variable set. It should point to main Java install directory which contains bin, lib and other directories.
After that, you need to download Groovy binaries from Groovy download site. I suggest to download zipped binaries. This file is suitable for all operating systems, as libraries are all jar files, and binaries contain both windows batch files and Unix/Linux shell scripts. All you need to do after that is unzip the file wherever you find suitable.
Final step is adjusting your PATH environment variable. It should contain both Java binaries directory and Groovy binaries directory (e.g. /usr/java/bin and /usr/groovy/bin).
You can check if all is set well by executing the command “groovy” from the command line. It should output standard usage message, something like this:
[~]> groovy error: neither -e or filename provided usage: groovy [options] [args] options: -a,--autosplit <splitPattern> split lines using splitPattern (default '\s') using implicit 'split' variable -c,--encoding <charset> specify the encoding of the files -D,--define <name=value> define a system property -d,--debug debug mode will print out full stack traces -e <script> specify a command line script -h,--help usage information -i <extension> modify files in place; create backup if extension is given (e.g. '.bak') -l <port> listen on a port and process inbound lines -n process files line by line using implicit 'line' variable -p process files line by line and print result (see also -n) -v,--version display the Groovy and JVM versions
So now we can start groovying. As a teaser, here’s a readable example: calculate bufwaits ratio based on onstat -p output (version 11.50xC9):
groovy -e "out='onstat -p'.execute().in.text.readLines(); \ pg=out.tokenize().toLong(); \ bwr=out.tokenize().toLong(); \ bwai=out.tokenize().toLong(); \ println '\nBufwaits Ratio: ' + ((pg+bwr)>0 ? (bwai*100)/(pg+bwr) : 0)"
Although Groovy doesn’t require a semicolon at the end of the statement, it has to be used here to delimit the statements because this is actually one-liner. You could make a script out of it, bw_ratio.groovy, with the same contents:
out='onstat -p'.execute().in.text.readLines() pg=out.tokenize().toLong() bwr=out.tokenize().toLong() bwai=out.tokenize().toLong() println '\nBufwaits Ratio: ' + ((pg+bwr)>0 ? (bwai*100)/(pg+bwr) : 0)
and execute it:
[~/]> groovy bw_ratio.groovy Bufwaits Ratio: 0.0602958416
In addition to Groovy home page with loads of examples and information, there are many sites dedicated to this powerful technology, just google for Groovy. There are some cool books about it, but I’d definitely recommend “Groovy in Action” authored by Dierk Konig.