Groovy Beginners Tutorial

Installation

The only prerequisite for installing Groovy is that you must first have a 1.4 or 1.5 or 1.6 JDK installed (available free from http://java.sun.com/j2se/) and the JAVA_HOME environment variable set to the location of your JDK installation.

To install Java:

  • Get the Java distribution.
  • Run the installer.
  • Set the JAVA_HOME environment variables. On Windows, follow these steps:
  • Open the System control panel
  • Click the Advanced tab
  • Click the Environment Variables button
  • Add a new System variable with the name JAVA_HOME and the value of the directory Java was installed in (mine is C:\Program Files\Java\jdk1.***)
  • Optionally add %JAVA_HOME%\bin to your system path

To install Groovy:

  1. Grab the latest stable release from .
  2. Unzip the file to a directory on your filesystem; for example, C:\groovyor /home/username/groovy.
  3. We recommend checking whether you have a CLASSPATH variable set inyour environment and unsetting it, at least temporarily, to avoid classpath problems, especially if you have any problems running basicGroovy commands.
  4. Optionally, set an environment variable GROOVY_HOME whose value is set tothe location where you unzipped the distribution zip file. If you are runningyour Groovy commands from the standard install location, youshould not need to set this variable.
  5. Optionally, include GROOVY_HOME/bin in your PATH environment variable.This will make all the Groovy command-line tools available in your path.
  6. Test your installation by executing groovysh from a command line-shell.You
    should see output like this:
>groovysh

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

groovy>exit

Congratulations! That is it for installing. Type exit to end the groovysh program. That’s all there is. It looks minimal, and that’s on purpose. You don’t have to deal with any specifics of your operating system, such as messing with the quagmire that is Windows registry. If you decide to “uninstall” Groovy, just delete the GROOVY_HOME directory, and you’re done.

Running Groovy

First, we need to introduce you to the tools you’ll be using to run and optionally compile Groovy code. If you want to try these out as you read, you’ll need to have Groovy installed, of course.
There are three commands to execute Groovy code and scripts, as shown below in table. Each of the three different mechanisms of running Groovy is demonstrated in the following sections with examples and screenshots. Groovy can also be “run” like any ordinary Java program.

Command

What it does

groovysh Starts the groovysh command-line shell, which is used to execute Groovy code interactively. By entering statements or whole scripts, line by line, into the shell and giving the go command, code is executed “on the fly.”
groovyConsole Starts a graphical interface that is used to execute Groovy code interactively; moreover, groovyConsole loads and runs Groovy script files.
Groovy Starts the interpreter that executes Groovy scripts. Single-line Groovy scripts can be specified as command-line arguments.

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

groovy>

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 (currentlydisabled)

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"

Using groovy Console

The groovy Console 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, groovy Console is written in Groovy.
The groovy Console takes no arguments and starts a two-paneled Window like the one shown in figure below. 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 groovy Console’s File menu has New, Open, Save, and Exit commands. New opens a new groovy Console 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 groovy Console.
That’s it for groovy Console. Whether you prefer working in groovysh or groovy- Console is a personal choice. Script programmers who perform their work in command shells tend to prefer the shell.

Using groovy

The groovy command is used to execute Groovy programs and scripts.Copy the code given below into a file, and save it as Fibonacci.groovy. The file extension does not matter much as far as the groovy executable is concerned, but naming Groovy scripts with a .groovy extension is conventional. One benefit of using an extension of .groovy is that you can omit it on the command line when specifying the name of the script—instead of groovy MyScript.groovy, you can just run groovy MyScript.

current = 1

next = 1

10.times {                //Loop to times

print current + ' '

newCurrent = next

next = next + current

current = newCurrent

}

println ''

Run this file as a Groovy program by passing the file name to the groovy command. You should see the following output:

>groovy Fibonacci

1 1 2 3 5 8 13 21 34 55

The groovy command has many additional options that are useful for command line scripting. For example, expressions can be executed by typing groovy –e “println 1+1”, which prints 2 to the console.

3 Comments

  1. quite simple tutorial.

    first release : 2016-08-12T08:45:05+00:00
    last update : 2017-07-21T21:42:27+00:00

    even after 1 year you’re just updating same level of tutorial!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.