Saturday, June 01, 2013

Groovy Basics: Hello World Example

In Dynamic Language, always remember one Rule of Thumb.

* When you're unsure what type you want your Object, always use the 'dynamically typed' keyword.

In Groovy, def simply means 'dynamically typed'.

As the Groovy Site says a dynamic language for the VM you must always remember Groovy Syntax feels Dynamic whereas once it's compiled it's the same Class File that Java Compiler (javac) Generates as Groovy runs on the VM and VM doesn't have a support for dynamic language. You will find Groovy Syntax Much Clear and More Readable as you write less code.

A Groovy File can either Contain Scripts, Class Definition or both. I usually don't prefer mixing Scripts and Class Definitions in a same file. I will come to this later.

You can check one-liner on the Groovy Shell instead of writing a Script file.

On Console/Prompt Type:

groovysh // It will open a Groovy Shell.

groovy:000> println "Hello, World."
Hello, World.
===> null

OR Simply,

groovy:000> "Hello, World."
===> Hello, World.

You will see the shell doesn't exit after the execution instead it's asking for your input again. This is called REPL - Read Evaluate Print Loop. You can type exit, quit, \q or \x to exit the shell.

You can also use the groovy command instead of going into the shell.

groovy -e "println 'Hello, World.'"

Here, -e <script> specify a command line script.

You can write that line in a new file and save it as HelloWorldScript.groovy try to run it,

groovy HelloWorldScript.groovy


groovy HelloWorldScript

A Groovy File can have any of the below mentioned extension. You can run the groovy command without extension in which case groovy will look for all the files in your CLASSPATH with one of the below mentioned extention and .class extention as well once it's found, it will execute that or will throw an error in case file with the same name doesn't exist in the CLASSPATH.


The groovy command runs your code without generating .class files. If you use groovyc compiler to explicitly compile your files first and then run them using the groovy command. You can do it. What if you made changes to your groovy source file and forgot to compile them? What will happen? No Worries! The groovy command is smart enough to load and run the right files for you that means suppose you make any changes to your source file but forgot to generate the .class file using the groovyc command (groovyc). the groovy command will ignore the .class file present in the path and will run the source file instead.

Now, Lets write a first HelloWorld.groovy program using the class definition,

You might notice that there are no import statements, parentheses, package prefix or a semi-colon.
Point to Remember: Like Java imports java.lang.* package by default Groovy imports the following packages by default. groovy.lang.*, groovy.util.*, java.lang.*, java.util.*,*,* and classes java.math.BigInteger and java.math.BigDecimal.

Another Point to remember here that Script files don't have a main method but they are still executed, As Each Script file turned into a Java class with the same name as filename that extends the groovy.lang.Script which contains the main method so the runtime can execute it.

As, I discussed earlier not to mix Scripts and Class Definitions in a Single file. Here is why,

Let me explain if the above error is still not clear. What Groovy does here is when it sees a Script inside a source file, it tries to create a class with the same name as source filename which in this case is HelloWorld. Their is a class definition inside the source file which has a same name i.e., HelloWorld for which a class will be created but when it comes to the Script it will fail to create as the class HelloWorld already exist.

How to fix?

You can fix this either by changing the Source Filename or Change the Class Name to make it run. Always try not to mix the Class Definition and Script in a same file.

As, I mentioned above that you need a Java VM to run the Groovy code you can run them directly using the Java Command. All you require is a single Groovy Jar in your classpath.

Compile and Run

First you have to generate a .class file using the Groovy Compiler.

Window Users:

C:\GROOVY-DEMOS>groovyc HelloWorld.groovy

Linux Users:

$ groovyc HelloWorld.groovy

To Run,

Window Users:

C:\GROOVY-DEMOS>java -classpath %CLASSPATH%;%GROOVY_HOME%\embeddable\groovy-all-2.1.3.jar HelloWorld

Linux Users:

$ java -classpath %CLASSPATH%:%GROOVY_HOME%/embeddable/groovy-all-2.1.3.jar HelloWorld

Note: Never put the groovy-all-<VERSION>.jar in your classpath, it will effect the groovysh command. If you get an error while running the groovysh command make sure you don't have groovy-all-<VERSION>.jar in your classpath.

If you are looking for editors/IDE with Groovy Support try Sublime Text 2 or IntelliJ Idea.

Hope this Helps!
Please leave your comments


Post a Comment