What is Groovy? Getting Started with Groovy - A tutorial

By: Whitey  

If you are a Java developer, or any programmer who has written code in Java, you know that although Java is a very powerful language, some things are easier than others. For example, creating structure and objects through classes is great in Java, but file I/O can be a real hassle. In those cases, a dynamic language with features akin to Ruby, Python, or other scripting languages would be a great help. That's where Groovy comes in. Groovy is a somewhat recent development that allows Java programmers to easily script functionality into their programs and improve productivity. The purpose of this tutorial is to introduce Java programmers to Groovy through the traditional "Hello World" application and encourage further exploration of this language. So, without further ado, lets begin...

First, before you start writing any code, download the Groovy Development Kit(GDK) from here. Next, install the GDK using these instructions:

Groovy requires Java, so you need to have a version available (1.4 or greater is required). Here are the steps if you don't already have Java installed:

* Get the latest Java distribution from the Developer Resources for Java Technology website.
* Run the installer.
* Set the JAVA_HOME environment variables. On Windows, follow these steps:
o Open the System control panel
o Click the Advanced tab
o Click the Environment Variables button
o 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.5.0_04)
o Optionally add %JAVA_HOME%\bin to your system path
(Note: as an alternative to setting a system environment variable, you can create yourself a '.bat' or '.cmd' file which sets the variable. You then need to run that batch file in any console window in which you wish to run Java and double clicking on '.bat' or '.cmd' files containing Java invocation instructions won't work. If you are unsure about what this means, follow the earlier instructions.)

Download the Groovy installer or binaries from the downloads page and follow the installation instructions. (There is currently an issue where you cannot have spaces in the path where Groovy is installed under windows. So, instead of accepting the default installation path of "c:\Program Files\Groovy" you will want to change the path to something like "c:\Groovy")


* Get a copy of the Groovy distribution from the website, and copy it to some place on your hard drive.
* Unzip the groovy archive to some logical place on your hard drive, I have mine in C:\dev\groovy-1.0-jsr-06
* Set the GROOVY_HOME environment variables. On Windows, follow these steps:
o Add a new System variable with the name GROOVY_HOME and the value of the directory groovy was installed in (mine is C:\dev\groovy-1.0-jsr-06)
o Start a command prompt, and type "set" and hit return to see that your environment variables were set correctly.
* Optionally add %GROOVY_HOME%\bin to your system path
* Try opening groovyConsole.bat by double clicking on the icon in the bin directory of the Groovy distribution. If it doesn't work, open a command prompt, and change to the bin directory and run it from there to see what the error message is.

Now that you have installed the GDK on your computer, lets start writing come code. Open up the text editor of your choice, or the groovyConsole included with the Groovy download. Since most Java code is valid Groovy code, we will start with something very recognizable to most Java developers, the "Hello World" program. Type in the following code and compile with Groovy:


public class Main
	public static void main(String[] arguments)
		System.out.println("Hello World");

This code works, but it isn't very Groovy. So, lets change it a bit.


public class Main
	public static void main(String[] arguments)
		println "Hello World"

We made two changes to the code above. The first one is the most obvious. Instead of writing System.out.println() every time you want to display information, in Groovy you only have to write println "". Next, we removed the semicolon at the end of the line. In Groovy, much like in JavaScript, semicolons are optional. We can change this code further however.


println "Hello World"

Whoa! What just happened! Are you telling me that you don't even need a class to make the Groovy code work!? That is exactly correct. Since Groovy is inherently a scripting language, it does not need the restrictions or boundaries of classes. The code actually produces "Hello World" when you run it. Try it for yourself.
Besides the basic features presented in this tutorial, Groovy has a wealth of functionality to bring to the table. Things like closures, Domain Specific Language capabilities, and a massive simplification of most anything you can think of. I encourage any Java developer out there to take a look at Groovy's Homepage for tutorials, usage guides, and tons of other information regarding Groovy. So give this great language a shot and see what groovy stuff you can come up with.

Archived Comments

1. FUCK YOU...with due respect...!!!
View Tutorial          By: sundar ram at 2016-10-13 20:47:22

2. just I am need of some of the soft copy related to groovy scripting
View Tutorial          By: shani at 2015-08-12 06:01:05

3. Nice tutorial on preparing to setup for groovy programming
View Tutorial          By: grails cookbook at 2013-10-07 07:38:17

4. im jst a begnr of it
and i hope it wl hlp me out.........

View Tutorial          By: nitu at 2011-12-08 10:08:30

Most Viewed Articles (in Trends )

Latest Articles (in Trends)

Comment on this tutorial