Beginner

Hello World in Groovy

Your first Groovy program - the classic Hello World example with Docker setup on macOS, Windows, and Linux

Introduction

Welcome to your first Groovy program! In this tutorial, we’ll create the classic “Hello, World!” example using Groovy, a powerful dynamic language for the Java Virtual Machine (JVM). One of Groovy’s greatest strengths is how it simplifies Java—what takes multiple lines in Java can often be done in a single line in Groovy.

We’ll use Docker to run our Groovy program, ensuring it works consistently across macOS, Windows, and Linux without requiring a local Groovy installation.

Why Groovy for Hello World?

Groovy makes “Hello, World!” remarkably simple:

  • No class or method boilerplate required
  • No semicolons needed
  • No explicit compilation step
  • Just pure, readable code

Compare this to Java, which requires a class definition, a main method, and System.out.println. Groovy lets you write println "Hello, World!" and you’re done!

The Code

Create a file named hello.groovy:

1
println "Hello, World!"

That’s it! This single line is a complete Groovy program. Let’s break down what makes this work:

Understanding the Code

println - This is Groovy’s built-in print function that automatically adds a newline. In Java, you’d need System.out.println(), but Groovy provides this as a global function for convenience.

String without quotes - In Groovy, when you have a simple string without special characters, you can even omit the quotes in many contexts. However, it’s generally better practice to include them.

No semicolons - Groovy makes semicolons optional, reducing syntactic noise.

No class or main method - Groovy allows you to write script-style code without the ceremony of class definitions. Behind the scenes, Groovy wraps this in a Script class, but you don’t need to worry about that.

Alternative Syntax

Groovy is very flexible. Here are equivalent ways to write Hello World:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
// Using parentheses (more Java-like)
println("Hello, World!")

// Using System.out explicitly (Java style)
System.out.println("Hello, World!")

// Using print with explicit newline
print "Hello, World!\n"

// Using string interpolation (GString)
def message = "World"
println "Hello, ${message}!"

All of these produce the same output, demonstrating Groovy’s flexibility in accommodating different coding styles.

Running with Docker

Docker provides a consistent environment to run Groovy code without installing anything locally except Docker itself.

Step 1: Pull the Docker Image

First, download the official Groovy Docker image:

1
docker pull groovy:4.0-jdk17-alpine

This image contains:

  • Groovy 4.0: The latest major version with modern features
  • JDK 17: Java Development Kit 17, providing the JVM runtime
  • Alpine Linux: A minimal Linux distribution, keeping the image small

Step 2: Run Your Program

Execute your Groovy script using Docker:

1
docker run --rm -v $(pwd):/app -w /app groovy:4.0-jdk17-alpine groovy hello.groovy

Let’s break down this command:

  • docker run - Execute a command in a new container
  • --rm - Automatically remove the container when it exits (keeps your system clean)
  • -v $(pwd):/app - Mount your current directory to /app in the container
    • $(pwd) expands to your current working directory
    • On Windows PowerShell, use ${PWD} instead
  • -w /app - Set the working directory inside the container to /app
  • groovy:4.0-jdk17-alpine - The Docker image to use
  • groovy hello.groovy - The command to execute inside the container

Expected Output

Hello, World!

That’s all! You should see the classic greeting printed to your terminal.

What Makes This Different from Java?

If you’re coming from Java, you might be surprised by how simple this is. Let’s compare:

Java Hello World:

1
2
3
4
5
public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello, World!");
    }
}

Groovy Hello World:

1
println "Hello, World!"

Groovy eliminates all the boilerplate while maintaining full Java compatibility. Under the hood, Groovy is generating bytecode that runs on the same JVM as Java, but you don’t have to write all that ceremony.

Next Steps

Now that you’ve run your first Groovy program, you can explore:

  1. Variables and Types - Learn about Groovy’s dynamic and static typing
  2. Collections - Groovy’s powerful list and map syntax
  3. Closures - First-class functions that make Groovy expressive
  4. Java Integration - Call any Java library from Groovy
  5. Metaprogramming - Modify classes at runtime

Troubleshooting

Docker Not Found

If you get a “docker: command not found” error:

  1. Install Docker Desktop from docker.com
  2. Start Docker Desktop
  3. Verify installation: docker --version

Permission Denied (Linux)

On Linux, if you get permission errors:

1
2
3
4
5
# Add your user to the docker group
sudo usermod -aG docker $USER

# Log out and back in, then test
docker run hello-world

Windows Path Issues

On Windows, if $(pwd) doesn’t work:

PowerShell:

1
docker run --rm -v ${PWD}:/app -w /app groovy:4.0-jdk17-alpine groovy hello.groovy

Command Prompt:

1
docker run --rm -v %cd%:/app -w /app groovy:4.0-jdk17-alpine groovy hello.groovy

Script Not Found

If you get “script not found”:

  1. Verify the file exists: ls hello.groovy (or dir on Windows)
  2. Make sure you’re in the correct directory
  3. Check the filename spelling

Understanding Groovy’s Design

Groovy was designed with several goals in mind:

  1. Java Compatibility - Any valid Java syntax is valid Groovy syntax
  2. Reduce Boilerplate - Make common tasks require less code
  3. Dynamic Features - Support scripting-style rapid development
  4. Optional Static Typing - Use types when you need them, omit them when you don’t
  5. Powerful DSLs - Enable creation of domain-specific languages

This Hello World example demonstrates the “reduce boilerplate” goal perfectly. As you continue learning Groovy, you’ll discover how these design principles make development faster and more enjoyable while maintaining the safety and ecosystem of the JVM.

Conclusion

Congratulations! You’ve successfully run your first Groovy program using Docker. This simple example demonstrates Groovy’s philosophy: keep things simple and remove unnecessary complexity. You’ve experienced how Groovy eliminates Java’s boilerplate while maintaining full compatibility with the JVM ecosystem.

The power of Groovy comes from this balance—it’s as simple as a scripting language when you want it to be, but has the full power of Java when you need it. As you continue your Groovy journey, you’ll discover how this flexibility makes it excellent for everything from quick scripts to large-scale applications.

Running Today

All examples can be run using Docker:

docker pull groovy:4.0-jdk17-alpine
Last updated: