Close

Getting Started with Groovy

[Updated: Nov 10, 2018, Created: Nov 7, 2018]

Apache Groovy is dynamic language for Java Virtual Machine. It increases developer productivity by reducing boilerplate code. It optionally allows static typing. Most of the Java code can still be run with Groovy.

This series of tutorials is quick walk through of Groovy language. The tutorials assume that you know the Java language already.

Following is a quick getting started with Groovy example.

Downloading and Installing

  1. Download Groovy from here (zip distribution).
  2. Extract the zip file. Set GROOVY_HOME environment variable (the root directory) and GROOVY_HOME/bin to the system path.
  3. JAVA_HOME should also be set. If you want to use Java version other than the one on your classpath, set JAVA_HOME in GROOVY_HOME/bin/startGroovy.bat.

In this tutorial we are using Groovy 2.5.3 with Java 11.

If you are also going to use Groovy with Java 9/10/11 then you should set GROOVY_TURN_OFF_JAVA_WARNINGS=true in GROOVY_HOME/bin/startGroovy.bat, otherwise you will get 'illegal reflective access' warnings (due to Java 9 module changes).

After installation:

C:>groovy -v
Groovy Version: 2.5.3 JVM: 11 Vendor: Oracle Corporation OS: Windows 10

Hello world Example

Create a groovy script file with any name, say hello.groovy and add following line and save:

hello.groovy

println "Hello groovy!"

The above groovy code can be run by using command line tool groovy.
The complete command will be groovy hello.groovy or groovy hello:

D:\groovy-hello-world>groovy hello
Hello groovy!

Compare above code to the Java way of creating class and a static main method to achieve the same thing.
Also System.out.println("Hello groovy!") has reduced to just println "Hello groovy!".
Use of the terminating semi colon is optional.

Using Groovy console

Groovy console (comes with Groovy distribution) is a Java Swing application where we can write and run code quickly.

From command line:

D:\groovy-hello-world>groovyconsole

Using groovyc

In the first example above, we ran the groovy script directly via groovy command. We can also compile it into .class file by using groovyc:

D:\groovy-hello-world>groovyc hello.groovy

That will create hello.class file:

D:\groovy-hello-world>dir /b
hello.class
hello.groovy

What groovy code compiles to?

Let's use javap to see what's in it:

D:\groovy-hello-world>javap hello.class
Compiled from "hello.groovy"
public class hello extends groovy.lang.Script {
public static transient boolean __$stMC;
public hello();
public hello(groovy.lang.Binding);
public static void main(java.lang.String...);
public java.lang.Object run();
protected groovy.lang.MetaClass $getStaticMetaClass();
}

That shows that above groovy script is nothing but syntactic sugar which compiled to a normal Java class with main method. The compiled code also has some Groovy API usage. The body of the script is copied into the run method.

Following is the equivalent Java Code:

import org.codehaus.groovy.runtime.InvokerHelper;

public class hello extends groovy.lang.Script {
    public Object run() {
        System.out.println("Hello groovy!");
        return null;
    }

    public static void main(String[] args) {
        InvokerHelper.runScript(hello.class, args);
    }
}

Running compiled class

The above class file can be run with java command:

D:\groovy-hello-world>java -cp .;%GROOVY_HOME%\lib\* hello
Hello groovy!
WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by org.codehaus.groovy.vmplugin.v7.Java7$1 (file:/D:/groovy/apache-groovy-sdk-2.5.3/groovy-2.5.3/lib/groovy-2.5.3.jar) to constructor java.lang.invoke.MethodHandles$Lookup(java.lang.Class,int)
WARNING: Please consider reporting this to the maintainers of org.codehaus.groovy.vmplugin.v7.Java7$1
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release

The above warnings are due to Java 9 module changes which we mentioned before (also check out Reflective access in Java 9). To avoid those warning set following java options:

D:\groovy-hello-world>java --add-opens=java.base/java.lang=ALL-UNNAMED --add-opens=java.base/java.lang.invoke=ALL-UNNAMED -cp .;%GROOVY_HOME%\lib\* hello
Hello groovy!

The groovy command, which we used the first time above, also goes through the same process, i.e. it compiles groovy code to .class first, keeps it in memory, and then uses java command (like we did just above) to run that.

See Also