Skip to content
Tutorials Center
My learning

Topics

Lesson 2 of 38 ยท Getting Started

Install and Run Groovy

Install Groovy with SDKMAN or a package manager, then run code with groovy, groovysh and the Groovy console. Learn scripts, shebang lines, arguments and the classpath.

To write Groovy on your own machine you need a Java Development Kit (JDK) and the Groovy distribution. Both are easy to install, and once they are, you get four ways to run code: one-liners, script files, an interactive shell and a small graphical console. This lesson walks through all of them.

You cannot run Groovy on this website, so the commands below are meant for your own computer. After this lesson you will be able to run every Groovy example from the course locally.

Step 1: a JDK

Groovy runs on the JVM, so first check that Java is installed:

Terminal
java -version

If you see a version number, you are set. Groovy 4 and 5 work with current long-term-support JDKs such as 17 and 21. If the command is not found, install a JDK from your operating system's package manager or from a vendor such as Eclipse Temurin.

Step 2: install Groovy

With SDKMAN (Linux, macOS, Windows via WSL or Git Bash)

SDKMAN is a tool that installs and switches versions of JVM tools. It is the most flexible way to get Groovy:

Terminal
curl -s "https://get.sdkman.io" | bash
sdk install groovy
groovy --version

You can keep several versions side by side and switch with sdk use groovy <version>.

With a package manager

Terminal
brew install groovy        # macOS (Homebrew)
choco install groovy       # Windows (Chocolatey)
scoop install groovy       # Windows (Scoop)

From the zip file

Download the binary distribution, unzip it, set the GROOVY_HOME environment variable to the folder, and add GROOVY_HOME/bin to your PATH.

If groovy --version says the command is unknown right after installing, open a new terminal window so the updated PATH is loaded.

Four ways to run Groovy

flowchart TD
    G["Groovy installed"] --> A["groovy -e 'code' (one-liner)"]
    G --> B["groovy Script.groovy (file)"]
    G --> C["groovysh (interactive shell)"]
    G --> D["groovyConsole (GUI editor)"]

Pick whichever suits the task: quick checks, saved scripts, experiments or editing with a graphical window.

1. One-liners with -e

Terminal
groovy -e "println 'Hello, Groovy!'"

This prints Hello, Groovy!. It is the fastest way to test a single expression, for example groovy -e "println 2**10".

2. Script files

Save this as hello.groovy:

GROOVY
def name = 'World'
println "Hello, $name!"

Run it:

Terminal
groovy hello.groovy

The output is Hello, World!. A Groovy script needs no class and no main method. Groovy wraps the statements in a class named after the file and calls its run method for you.

3. The interactive shell: groovysh

Start groovysh and type expressions. Each line is evaluated at once, and the result is shown:

TEXT
groovy:000> 6 * 7
===> 42
groovy:000> [1, 2, 3].sum()
===> 6

Useful shell commands start with a colon: :help lists them, :clear resets the buffer, :exit quits. The shell is perfect for exploring the API of an object: type 'abc'. followed by the Tab key to see the available methods.

4. The console: groovyConsole

groovyConsole opens a small window with an editor on top and output below. Press Ctrl+R (Cmd+R on macOS) to run the script. It is handy for experiments that span several lines, and it can show the object inspector and AST browser later in your learning.

Command-line arguments

Scripts receive their arguments in an implicit variable named args:

GROOVY
// greet.groovy
if (args.length == 0) {
    println 'Usage: groovy greet.groovy <name>'
} else {
    println "Hello, ${args[0]}!"
}

Running groovy greet.groovy Ada prints Hello, Ada!. The Java stand-in below mimics the same pattern with a main method so you can see how arguments are checked.

Java Java stand-in: reading arguments like a Groovy script

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

The shebang line

On Linux and macOS you can make a Groovy script executable. Put this as the very first line:

GROOVY
#!/usr/bin/env groovy
println 'I run directly'

Then chmod +x script.groovy and run it with ./script.groovy. The #! line is legal in Groovy, which ignores it as a comment.

Using libraries: @Grab

A quick script often needs a library. Groovy can download dependencies for you with the @Grab annotation, using the Grape system:

GROOVY
@Grab('org.apache.commons:commons-lang3:3.14.0')
import org.apache.commons.lang3.StringUtils

println StringUtils.capitalize('groovy')

The first run downloads the jar to a local cache (~/.groovy/grapes); later runs reuse it. For real projects you will use a build tool instead (see the build chapter later in the course), but @Grab is excellent for self-contained scripts.

You can also add jars manually with the -cp option:

Terminal
groovy -cp lib/mylib.jar Script.groovy

Compiling to class files

Normally you do not need to compile. If you want class files, for example to ship to a Java application, use groovyc:

Terminal
groovyc Greeter.groovy
java -cp .:$GROOVY_HOME/lib/groovy-5.0.0.jar Greeter

(The jar name depends on your version. On Windows use ; instead of : in the class path.) Most developers let Gradle or Maven do this.

Editors and IDEs

IntelliJ IDEA (Community and Ultimate) has excellent Groovy support: completion, debugger, Gradle integration. VS Code has Groovy extensions that provide syntax highlighting and basic language support. Any plain text editor works too, because Groovy needs no special project structure.

A first complete session

Try this to confirm everything works:

Terminal
groovy --version
groovy -e "println 6 * 7"

The second command prints 42. For anything longer than one expression, a script file is clearer:

GROOVY
def squares = (1..5).collect { it * it }
println squares

Be careful with a space between a method name and a parenthesis: println (1+2).toString() calls println with (1+2).toString(), which may not be what you meant. When in doubt, drop the space or store the value in a variable first.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What do you need installed before Groovy will run?

    Groovy runs on the JVM, so a Java installation is required.
  2. 2. Which command runs a single line of Groovy without creating a file?

    The -e option evaluates the given text as a script. groovyc only compiles, and the other commands are not real.
  3. 3. How does a Groovy script read command-line arguments?

    Scripts get an implicit args array, like the parameter of Java's main method.
  4. 4. What does @Grab do?

    @Grab uses Grape to fetch a library from a Maven repository and put it on the class path before the script runs.

Summary

  • Install a JDK, then Groovy with SDKMAN, a package manager, or a zip file.
  • Run code with groovy -e, groovy file.groovy, groovysh or groovyConsole.
  • Scripts need no class or main; args holds command-line arguments.
  • A #!/usr/bin/env groovy line makes a script executable on Unix.
  • @Grab pulls in libraries for quick scripts; groovyc compiles to class files.

Next: Groovy vs Java