What is Groovy? Getting Started with Groovy - A tutorial

By: Whitey Emailed: 1678 times Printed: 2157 times    

Latest comments
By: rohit kumar - how this program is work
By: Kirti - Hi..thx for the hadoop in
By: Spijker - I have altered the code a
By: ali mohammed - why we use the java in ne
By: ali mohammed - why we use the java in ne
By: mizhelle - when I exported the data
By: raul - no output as well, i'm ge
By: Rajesh - thanx very much...
By: Suindu De - Suppose we are executing

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")

OR

* 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:

Code:

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.

Code:

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.

Code:

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.


Trends Home | All Trends Tutorials | Latest Trends Tutorials

Sponsored Links

If this tutorial doesn't answer your question, or you have a specific question, just ask an expert here. Post your question to get a direct answer.



Bookmark and Share

Comments(2)


1. View Comment

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
2. View Comment

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

Your name (required):


Your email(required, will not be shown to the public):


Your sites URL (optional):


Your comments:



More Tutorials by Whitey
What is Groovy? Getting Started with Groovy - A tutorial

More Tutorials in Trends
What is Hadoop?
Internet of Things
Browser Based Communications - WebRTC
Big Data - An Introduction
Will Apple open retail showrooms in India?
A comparison of VMware, Microsoft Hyper-V and Xen.
What is a chromebook?
Review: Ruggear - The world's most Rugged Phone in Singapore
What is Google Dart?
DKVM-8E Console Password (DLink KVM Switch)
What is Microsoft Office 365?
Migration from IPV4 to IPV6
What is PGP?
How to use GnuPG (GPG)?
The Failure of 2FA / Two-Factor Authentication

More Latest News
Most Viewed Articles (in Trends )
Introduction to Amazon Web Services
Different Types of cloud
What cloud service providers typically offer?
What is Groovy? Getting Started with Groovy - A tutorial
What is a chromebook?
Review: Ruggear - The world's most Rugged Phone in Singapore
What is Hadoop?
DKVM-8E Console Password (DLink KVM Switch)
What is cloud computing?
Communications-as-a-Service Solutions
Android Basics - An Introduction to Android
Internet of Things
How to use GnuPG (GPG)?
A comparison of VMware, Microsoft Hyper-V and Xen.
Big Data - An Introduction
Most Emailed Articles (in Trends)
What is SaaS?
Communications-as-a-Service Solutions
Internet of Things
What is Hadoop?
What is cloud computing?
Android Basics - An Introduction to Android
Google Wave - Get Ready for the next big wave from Google
What is Groovy? Getting Started with Groovy - A tutorial
A comparison of VMware, Microsoft Hyper-V and Xen.
What is Google Dart?
Different Types of cloud
What is Windows Azure?
What is IAAS?
What is Google Apps?
Google's Strategic Threat to Carrier Margins for New Services