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:
| |
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:
| |
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:
| |
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:
| |
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/appin 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/appgroovy:4.0-jdk17-alpine- The Docker image to usegroovy 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:
| |
Groovy 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:
- Variables and Types - Learn about Groovy’s dynamic and static typing
- Collections - Groovy’s powerful list and map syntax
- Closures - First-class functions that make Groovy expressive
- Java Integration - Call any Java library from Groovy
- Metaprogramming - Modify classes at runtime
Troubleshooting
Docker Not Found
If you get a “docker: command not found” error:
- Install Docker Desktop from docker.com
- Start Docker Desktop
- Verify installation:
docker --version
Permission Denied (Linux)
On Linux, if you get permission errors:
| |
Windows Path Issues
On Windows, if $(pwd) doesn’t work:
PowerShell:
| |
Command Prompt:
| |
Script Not Found
If you get “script not found”:
- Verify the file exists:
ls hello.groovy(ordiron Windows) - Make sure you’re in the correct directory
- Check the filename spelling
Understanding Groovy’s Design
Groovy was designed with several goals in mind:
- Java Compatibility - Any valid Java syntax is valid Groovy syntax
- Reduce Boilerplate - Make common tasks require less code
- Dynamic Features - Support scripting-style rapid development
- Optional Static Typing - Use types when you need them, omit them when you don’t
- 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