Setting up Groovy beginner tutorial with eclipse & command-line

Groovy is a general purpose scripting language which runs on the JVM, and can be viewed as a superset of Java. There are jobs requiring Java and Groovy skills. Any “.java” file can be renamed to “.groovy” and run via the groovy. Java needs two step process like “javac” to compile and “java” to run. But “.groovy” files can be compiled and run run with “groovy”. Groovy is a dynamic language like Python, Ruby, etc.

Step 1: Get Groovy support for eclipse: Get the Groovy libraries from “http://dist.springsource.org/snapshot/GRECLIPSE/e4.4”

Groovy for Eclipse IDE

Groovy for Eclipse IDE

restart eclipse after adding the Groovy support for eclipse.

Step 2: Create groovy project “HelloGroovy”: File -> New -> Other -> Groovy -> Groovy Project.

Groovy project within eclipe

Groovy project within eclipe

Groovy class

Groovy class

Step 3: Create the Groovy class “SimpleGroovy”: File -> New -> Other -> Groovy -> Groovy Class.

Groovy package and class

Groovy package and class

Step 4: Groovy code for “SimpleGroovy”

Isn’t Groovy a scripting language?

The above code can be simplified as shown below”. Because Groovy is a scripting language, there’s automatically a wrapping class (called Script). This wrapping class means that we can get rid of our own wrapping class, as well as the main method

The created project structure

Groovy project structure

Groovy project structure

Step 5: Groovy “console”

Right mouse click on “SimpleGroovy.groovy” as shown above and select “Run As” –> “Groovy Console”

Groovy Console

Groovy Console

You can also do “Run As” –> “Java Application”, etc.

Compiling and Running from the DOS command-line

Groovy needs Java and Groovy binary. Assumes that you already java Java installed and running. You need to download Groovy binary now.

Step 1: Download the Groovy binary: from http://groovy-lang.org/download.html.

Groovy binary download

Groovy binary download

Unzip the download into say “c:\tools”.

groovy binary download file structure

groovy binary download file structure

Step 2: Compile:

The compiled com\simple\groovy\SimpleGroovy.class will be in the “C:\Users\akumaras\workspace\HelloGroovy” folder

Step 3: Run:

Output:

Hello from Groovy

Any “.java” file can be renamed to “.groovy” and run via the groovy. Java needs two step process like “javac” to compile and “java” to run. But “.groovy” files can be compiled and run run with “groovy”.

Output:

Hello from Groovy

Note: You can also set the GROOVY_HOME environment variable and add it to the PATH variable.


800+ Java Q&As & tutorials

Top