Installation, Tools and First Program

2.1 Installing Groovy

Verify with groovy -v. Groovy needs a JDK (JAVA_HOME).

# SDKMAN (recommended on Linux/macOS/WSL)
curl -s "https://get.sdkman.io" | bash
sdk install java 21-tem
sdk install groovy           # latest stable
sdk install groovy 4.0.28    # specific version
sdk use groovy 5.0.0

# Other options
brew install groovy                  # macOS
choco install groovy                 # Windows (or scoop install groovy)
# Or download the binary zip from groovy.apache.org, set GROOVY_HOME, and add bin/ to PATH

2.2 The Groovy Tools

Run scripts (groovy script.groovy) or one-liners (groovy -e "println 1+1") Compile to .class files Interactive shell (REPL)

Swing GUI console with AST browser and script output Dependency manager for @Grab (third-party IDE features)

2.3 Hello, World

The equivalent class-based program:

// hello.groovy: a script needs no class or main method
println "Hello, World!"
groovy hello.groovy
groovy -e 'println "Hello from the command line"'
groovyc hello.groovy && java -cp ".:$GROOVY_HOME/lib/*" hello
class Hello {
    static void main(String[] args) {
        println "Hello, World!"
    }
}

2.4 Interactive Shell

groovysh
groovy:000> def x = [1, 2, 3]
groovy:000> x.sum()
===> 6
groovy:000> :help

2.5 IDE Setup

IntelliJ IDEA (Community or Ultimate) has first-class Groovy support: debugging, refactoring, Gradle/Spock integration.

VS Code: the "Groovy Language Server" / "code-groovy" extensions (basic). Eclipse: Groovy Development Tools (GDT).

2.6 Reading Input and Arguments

def name = System.console()?.readLine("Name: ") ?: System.in.newReader().readLine()
println "Hello, $name"
println "Args: ${args}"         // scripts get an implicit `args` variable