Friday, 25 March 2016

An Introduction to Groovy

Java is a very powerful language and can do wonders. But it is not suitable for scripting. You can do everything using Java but at times you may have felt that other languages (python, Ruby etc.) are better. To complement the areas usually Java lack, Groovy comes to play.

Although written in Java and Groovy, sometimes it gives immense support to developers. Otherwise, developers would have to write lengthy programs to achieve the same result. This way it complements Java. So, we can think Groovy as a companion of Java to ease developers' life.

In a nutshell, although not necessary all the time, Groovy is a Good To Have knowledge for Java Developers.

At this point, I will assume that you know Java and have some experience with it. So, I will skip some basic stuffs like what a computer program is, different types of languages and all these stuffs and usually Groovy and Java goes side by side. So, we may encounter some Java programs as well.

I will directly start with Groovy Installation, that too not in a great detail.

This is the official website of Groovy, http://www.groovy-lang.org/, I suggest you to take a look on it and grab the adequate download for your system from here, http://www.groovy-lang.org/download.html

Once you have configured your system, come back and let's enjoy our time with Groovy.

Once the installation is complete, you can use Groovy in following ways,

On console - groovysh

Many of the programmers use console for compiling, running and deploying their programs or archives. So, our first approach to Groovy will be using our very known console.

I assume that, you already have added groovy to your path and can run groovy from wherever you want.
On your console, just type
palash@ubuntu:~$ groovysh
Groovy Shell (1.8.6, JVM: 1.8.0_74)
Type 'help' or '\h' for help.
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------
groovy:000>

Now, Groovy has successfully running and waiting for you to key in some commands. Let's greet the world like any traditional programming language's first program,
groovy:000> println "Hello World"
Hello World
===> null
groovy:000> 

Yes, that's what you see as the output.
You might be thinking, "Where does this null come from ???"

Well, remember, System.out.println() is a void method. If still you cannot figure it out, just leave it for now, we'll discuss this later.


After the console way, we will now check the way we generally run a Java Program.

Using groovy Command

To run a java program, we use java. If you need to refresh this knowledge, take a look on this article.

Similar to Java, we run groovy scripts using groovy command.  For a single line program, we can use groovy command with -e.
Let's see both the approaches,
palash@ubuntu:~/git_repo/groovy/groovy/src$ vi hello_world.groovy
palash@ubuntu:~/git_repo/groovy/groovy/src$ groovy hello_world.groovy 
Hello World
palash@ubuntu:~/git_repo/groovy/groovy/src$ 

Simply, I've written a script that holds the following line,
println "Hello World"

I've saved the file with the name hello_world.groovy and in the last line, I've run the script using groovy command with the file name as the argument.

You might have noticed that this program does not show null after the out put of println.
try to figure out why. Otherwise, leave it for now.

Now, this is too much if you have to run a single line program, you have to use a text editor, save the program and run this using groovy.

You might think, do I really need to do this ?

Well, it depends, if you want this single line code snippet to run multiple times, you might consider it to be saved on the disk but if you think that, I will run this just a single time, well you have luck in that case as well. You may simply ignore it to be saved and can get the work done in the console command itself.

Let me show you with the same code snippet again but without using a text editor,
palash@ubuntu:~$ groovy -e 'println "Hello World""'
Hello World
palash@ubuntu:~$ 

That's pretty much what you can do without the text editor as well.

The groovyConsole

With the installation comes a simple Java Swing based Groovy editor, known as groovyConsole. Not a lot features, simple text editor with extension to run groovy script. Let me show you the snapshots of the GUI where I've written the most simple Groovy Class and run it.


Well, you may complain that, this is a Java Program and Not a Groovy Program. Well, yes, it is a simple Java Program which we've run using groovy.
In fact, a valid Java Program is also a valid Groovy Program. So, it runs without any error.

And in fact, you can run Java in Groovy and Groovy in Java interchangeably. We'll see this later.

IDE Support
IntelliJ, Eclipse and NetBeans all have plug-in for Groovy. So, you can choose your IDE and run Groovy scripts from your IDE as well.

But as you already know that IDE is for the experts, when you have done significant amount of code on simple Text Editors and run them on console. So, recommended practice should avoid the IDE and work on vi, notepad, notepad++, console and groovyConsole.

Source Code Distribution

Although the source code is available under Apache License, you should not use it to run the scripts directly. This defeats the purpose of learning. The source code of this article is available at,

HelloWorld.groovy
hello_world.groovy

You can download the code and verify with your own code.

Well, that's all I can put into the introductory article, the next article will be more on Groovy. What is it, how it came and its inventors, its support etc.