January 25, 2015
Hot Topics:

What Groovy Can Do For You

  • February 2, 2007
  • By Koenig with Glover, King, Laforge and Skeet
  • Send Email »
  • More Articles »

Using groovysh for "Hello World"

Let's look at groovysh first because it is a handy tool for running experiments with Groovy. It is easy to edit and run Groovy iteratively in this shell, and doing so facilitates seeing how Groovy works without creating and editing script files.

To start the shell, run groovysh (UNIX) or groovysh.bat (Windows) from the command line. You should then get a command prompt like this:

Lets get Groovy!
Version: 1.0-RC-01-SNAPSHOT JVM: 1.4.2_05-b04
Type 'exit' to terminate the shell
Type 'help' for command help
Type 'go' to execute the statements


The traditional "Hello World!" program can be written in Groovy with one line and then executed in groovysh with the go command:
groovy> "Hello, World!"
groovy> go

===> Hello, World!

The go command is one of only a few commands the shell recognizes. The rest can be displayed by typing help on the command line:

groovy> help
Available commands (must be entered without extraneous characters):
exit/quit - terminates processing
help - displays this help text
discard - discards the current statement
display - displays the current statement
explain - explains the parsing of the current statement (currently
execute/go - temporary command to cause statement execution
binding - shows the binding used by this interactive shell
discardclasses - discards all former unbound class definitions
inspect - opens ObjectBrowser on expression returned from
previous "go"

The go and execute commands are equivalent. The discard command tells Groovy to forget the last line typed, which is useful when you're typing in a long script, because the command facilitates clearing out the small sections of code rather than having to rewrite an entire script from the top. Let's look at the other commands.

Display command

The display command displays the last noncommand statement entered:

    groovy> display
1>	"Hello World!"
Binding command

The binding command displays variables utilized in a groovysh session. We haven't used any variables in our simple example, but, to demonstrate, we'll alter our "Hello World!" using the variable greeting to hold part of the message we print out:

groovy> greeting = "Hello"
groovy> "${greeting}, World!"
groovy> go

===> Hello, World!

groovy> binding
Available variables in the current binding
greeting = Hello

The binding command is useful when you're in the course of a longer groovysh session and you've lost track of the variables in use and their current values.

To clear the binding, exit the shell and start a new one.

Inspect command

The inspect command opens the Groovy Object Browser on the last evaluated expression. This browser is a Swing user interface that lets you browse through an object's native Java API and any additional features available to it via Groovy's GDK. Figure 1 shows the Object Browser inspecting an instance of String. It contains information about theString class in the header and two tables showing available methods and fields.

Figure 1: The Groovy Object Browser when opened on an object of type String, displaying the table of available methods in its bytecode and registered Meta methods .

Look at the second and third rows. A method with the name center is available on a String object. It takes a Number parameter (second row) and an optional String parameter (third row). The method's return type is a String. Groovy defined this new public method on the String class.

If you are anything like us, you cannot wait to try that new knowledge in the groovysh and type

groovy> 'test'.center 20, '-'
groovy> go

===> --------test--------

That's almost as good as IDE support!

For easy browsing, you can sort columns by clicking the headers and reverse the sort with a second click. You can sort by multiple criteria by clicking column headers in sequence, and rearrange the columns by dragging the column headers.

Future versions of the Groovy Object Browser may provide even more sophisticated features.

Using groovyConsole

The groovyConsole is a Swing interface that acts as a minimal Groovy interactive interpreter. It lacks support for the command-line options supported by groovysh; however, it has a File menu to allow Groovy scripts to be loaded, created, and saved. Interestingly, groovyConsole is written in Groovy.

The groovyConsole takes no arguments and starts a two-paned Window like the one shown in figure 5. The console accepts keyboard input in the upper pane. To run a script, either key in Ctrl+R, Ctrl+Enter or use the Run command from the Action menu to run the script. When any part of the script code is selected, only the selected text is executed. This feature is useful for simple debugging or single stepping by successively selecting one or multiple lines.

The groovyConsole's File menu has New, Open, Save, and Exit commands. New opens a new groovyConsole window. Open can be used to browse to a Groovy script on the file system and open it in the edit pane for editing and running. Save can be used to save the current text in the edit pane to a file. Exit quits the groovyConsole.

The Groovy Object Browser as shown in figure 2 is equally available in groovyConsole and also operates on the last evaluated expression. To open the browser, press Ctrl+I (for inspect) or choose Inspect from the Actions menu.

Figure 2: The groovyConsole with a simple script in the edit pane that calculates the circumference of a circle based on its diameter. The result is in the output pane.

That's it for groovyConsole. Whether you prefer working in groovysh or groovyConsole is a personal choice. Script programmers who perform their work in command shells tend to prefer the shell.


I (Dierk) personally changed my habits to use the console more often for the sake of less typing through cut-and-paste in the edit pane.

Unless explicitly stated otherwise, you can put any code example in this article directly into groovysh or groovyConsole and run it there. The more often you do that, the earlier you will get a feeling for the language.

Page 2 of 4

Comment and Contribute


(Maximum characters: 1200). You have characters left.



Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Sitemap | Contact Us

Thanks for your registration, follow us on our social networks to keep up-to-date
Rocket Fuel