Monday, November 16, 2009

Groovy Console: Graphical Groovy Shell

I wrote about Groovy Shell in my last blog post. As I wrote in that post, Groovy Shell is a highly useful command line based method for experimenting with Groovy code. For those who prefer a graphical interface over the command line interface, Groovy Console provides a nice alternative and that is the subject of this blog post.

Assuming Groovy is appropriately installed on one's system with GROOVY_HOME defined correctly, it is easy to run the Groovy Console by simply typing groovyConsole at the command line. The next screen snapshot shows the running of Groovy Console along with the output from selecting Help->About.

It is easy to enter Groovy code and execute it (by clicking on the icon on the far right with the green arrow pointing right). The next two screen snapshots show entering Groovy code in the top pane and then the results of clicking on that "Execute Groovy Script" button in the bottom pane. Color coding helps make the scripts and the output more readable.

The Groovy Console supports larger fonts. The next screen image shows the results of selecting View -> Larger Font multiple times.

This image also shows a simple script for getting the date/time from Calendar using Groovy's property access syntax. Instead of needing to call getInstance() on Calendar and getTime() on the returned Calendar instance, I was able to access those directly with .instance.time.

Groovy Console allows one to select whether shortened or full stack traces should be displayed. The following two screen snapshots demonstrate the difference between the normal (shortened) stack traces and full stack traces. To turn on full stack traces, one selects View -> Show Full Stack Traces.

In this case, it is easy to deal with the stack trace. All that needs to be done is to import the Color class as shown in the next screen snapshot (classes in java.awt package are not automatically imported in Groovy).

Groovy Console supports numerous other useful features. Scripts can be saved to disk once they do what the developer wants. JAR files and directories can be added to the Groovy Console classpath for custom Java classes used by Groovy scripts. The Groovy Console page lists many more useful features of Groovy Console such as the ability to host Groovy Console as an applet and the ability to embed Groovy Console in Swing-based applications.

Another useful graphical approach to playing with Groovy code is the Groovy web console deployed on Google App Engine.

No comments: