10 Easy Groovy Hello World Examples for Beginners

Write your first Groovy hello world program with 10 practical hello world examples. Tested on Groovy 5.x with actual output. Complete beginner guide.

“The journey of a thousand lines of code begins with a single println.”

Lao Tzu, adapted for developers

Last Updated: March 2026 | Tested on: Groovy 5.x, Java 17+ | Difficulty: Beginner | Reading Time: 12 minutes

A groovy hello world program is the quickest way to verify your installation works and see how much cleaner Groovy is compared to Java. Groovy makes that first step surprisingly pleasant, regardless of your background with Java or JVM languages.

After this post, you will have written your first Groovy program, understood how Groovy differs from Java in its simplicity, and explored 10 different ways to print “Hello World” – from the dead-simple one-liner to more creative approaches using closures, GStrings, and even file output.

No prior Groovy experience needed. If you can open a terminal, you’re good to go. And once you’re done here, you might want to set up a proper Groovy IDE for your future projects.

What is Groovy?

Groovy is a dynamic, optionally-typed programming language that runs on the Java Virtual Machine (JVM). Think of it as Java’s more relaxed cousin – it does everything Java does, but with less boilerplate and more expressive syntax.

According to the official Groovy documentation, Groovy is “a powerful, optionally typed and dynamic language for the Java platform, with static-typing and static compilation capabilities.” That’s a fancy way of saying you get the best of both worlds – the freedom of dynamic typing when you want it, and the safety of static typing when you need it.

Key Points:

  • Runs on the JVM – full access to all Java libraries
  • Almost all Java code is valid Groovy code
  • Semicolons optional, type declarations optional
  • Native support for lists, maps, and regular expressions
  • Closures, GStrings, and metaprogramming built in
  • Powers the Grails web framework and Gradle build tool

Why Learn Groovy in 2026?

You might be wondering – with Kotlin, Scala, and a dozen other JVM languages out there – why pick Groovy? Here’s the thing: Groovy isn’t trying to replace Java. It’s trying to make your life easier when working with Java.

If you use Gradle, you’re already writing Groovy. If you use Jenkins, those pipeline scripts are Groovy. If you work with Grails or Spring Boot with Groovy, you’re deep in it. And with Groovy 5.x bringing modern features while keeping backward compatibility, it’s never been a better time to learn.

The learning curve from Java to Groovy is almost flat – your existing Java knowledge carries over directly. That’s not something you can say about Scala or Clojure.

Setting Up Groovy

Before writing your first Groovy program, you need Groovy installed. The quickest way is using SDKMAN (Linux/Mac) or downloading directly from the official Groovy download page.

Install via SDKMAN (Recommended)

Install Groovy via SDKMAN

sdk install groovy

Install on Windows

Download the Windows installer from the official site, or use Chocolatey:

Install Groovy on Windows

choco install groovy

Verify Installation

Check Groovy Version

groovy --version

Output

Groovy Version: 5.0.0 JVM: 17.0.x Vendor: Eclipse Adoptium

If you see a version number, you’re ready. For a detailed IDE setup guide, check out our Groovy IDE Setup post.

Your First Groovy Hello World

Here it is – the simplest possible Groovy hello world program. No class declaration, no main method, no semicolons. Just one line:

Hello World – One Line

println "Hello, World!"

Output

Hello, World!

That’s it. Save it as hello.groovy and run it with groovy hello.groovy. If you’re coming from Java, you probably just saved yourself about 5 lines of boilerplate. No public static void main(String[] args), no class wrapping, nothing.

Pro Tip: You can also run Groovy code directly from the command line without creating a file. Try groovy -e "println 'Hello, World!'" – perfect for quick experiments.

Syntax and Basic Usage

The println Method

The println method is Groovy’s built-in way to print text to the console followed by a newline. It’s shorthand for System.out.println() in Java.

println Syntax

// All of these are valid:
println "Hello"          // Parentheses optional for single argument
println("Hello")         // With parentheses - also fine
print "Hello"            // No newline at the end
println()                // Just prints a blank line

Running a Groovy Script

You have three ways to run Groovy code:

MethodCommandBest For
Script filegroovy hello.groovySaved programs
Command linegroovy -e "println 'Hi'"Quick one-liners
Groovy ConsolegroovyConsoleInteractive experimentation

10 Practical Hello World Examples

Let’s go beyond the basics. Here are 10 different ways to say “Hello, World!” in Groovy – each one teaching you something new about the language.

Example 1: The Classic One-Liner

What we’re doing: The simplest possible Groovy program – no class, no main method.

Example 1: Classic One-Liner

println "Hello, World!"

Output

Hello, World!

What happened here: Groovy scripts don’t need a class or main method. The script body itself is the entry point. The println method prints the text and adds a newline.

Example 2: Using GString Interpolation

What we’re doing: Using Groovy’s string interpolation to build the greeting dynamically.

Example 2: GString Interpolation

def name = "World"
println "Hello, ${name}!"

Output

Hello, World!

What happened here: Double-quoted strings in Groovy are GStrings – they support ${expression} interpolation. The variable name gets substituted right into the string. Single-quoted strings don’t do this – they’re plain Java strings.

Example 3: With a Method

What we’re doing: Wrapping the greeting in a reusable method.

Example 3: Using a Method

def greet(String who) {
    println "Hello, ${who}!"
}

greet("World")
greet("Groovy")
greet("TechnoScripts")

Output

Hello, World!
Hello, Groovy!
Hello, TechnoScripts!

What happened here: We defined a method using def and called it three times with different arguments. Notice how clean the method definition is – no access modifier needed, and Groovy’s def keyword handles the return type. Want to learn more about def? Check out Groovy Def Keyword.

Example 4: Using a Closure

What we’re doing: Assigning a greeting function to a variable using a closure.

Example 4: Closure

def greet = { name -> println "Hello, ${name}!" }

greet("World")
greet("Closure")

Output

Hello, World!
Hello, Closure!

What happened here: Closures are one of Groovy’s most useful features. They’re like anonymous functions you can store in variables, pass around, and call whenever you want. The -> arrow separates parameters from the body.

Did You Know? Closures are everywhere in Groovy – they power each(), collect(), findAll(), and almost every collection method.

Example 5: Java-Style with Class and Main Method

What we’re doing: Writing hello world the Java way – because all valid Java is valid Groovy.

Example 5: Java-Style

class HelloWorld {
    static void main(String[] args) {
        System.out.println("Hello, World!")
    }
}

Output

Hello, World!

What happened here: This is pure Java code – and it runs perfectly in Groovy. Every Java program is a valid Groovy program. But you’d never actually write it this way in Groovy. The one-liner version does the exact same thing with far less ceremony.

Example 6: Using a List and each()

What we’re doing: Greeting multiple people using a list and Groovy’s each() method.

Example 6: List with each()

def names = ["Alice", "Bob", "Charlie"]

names.each { name ->
    println "Hello, ${name}!"
}

Output

Hello, Alice!
Hello, Bob!
Hello, Charlie!

What happened here: We created a list with square brackets and iterated over it using each(). The closure { name -> ... } runs once for each element. This is the Groovy way to loop – clean, readable, and expressive.

Example 7: Using times() Loop

What we’re doing: Printing hello world a specific number of times using Groovy’s times() method.

Example 7: times() Loop

3.times {
    println "Hello, World!"
}

Output

Hello, World!
Hello, World!
Hello, World!

What happened here: The times() method on an integer runs a closure that many times. It’s way cleaner than writing a for loop just to repeat something. You can even use the iteration variable: 3.times { i -> println "Attempt ${i + 1}" }.

Example 8: Multiline String (Heredoc)

What we’re doing: Using triple-quoted strings to print a multiline greeting.

Example 8: Multiline String

def banner = """
====================
  Hello, World!
  Welcome to Groovy
====================
"""

println banner.trim()

Output

====================
  Hello, World!
  Welcome to Groovy
====================

What happened here: Triple double-quotes ("""...""") create multiline GStrings. They preserve line breaks and formatting, making them perfect for banners, templates, or SQL queries. We used trim() to remove the leading and trailing blank lines.

Example 9: Writing Hello World to a File

What we’re doing: Writing the greeting to a file instead of the console.

Example 9: Write to File

new File("hello.txt").text = "Hello, World!"
println new File("hello.txt").text

Output

Hello, World!

What happened here: Groovy adds a text property to Java’s File class. Setting it writes the content; reading it gives you the full file contents. No BufferedReader, no try-with-resources, no closing streams. One line each way. For more file operations, see our Groovy File Operations guide.

Example 10: Using a Map for Multilingual Hello

What we’re doing: Storing greetings in different languages using a Groovy map.

Example 10: Map-Based Greetings

def greetings = [
    english : "Hello, World!",
    spanish : "¡Hola, Mundo!",
    french  : "Bonjour, le Monde!",
    german  : "Hallo, Welt!",
    japanese: "こんにちは、世界!"
]

greetings.each { lang, greeting ->
    println "${lang.capitalize()}: ${greeting}"
}

Output

English: Hello, World!
Spanish: ¡Hola, Mundo!
French: Bonjour, le Monde!
German: Hallo, Welt!
Japanese: こんにちは、世界!

What happened here: Groovy maps are created with square brackets and colon-separated key-value pairs. The each() method on a map gives you both the key and value in the closure. And capitalize() is a Groovy method added to String that uppercases the first letter.

Pro Tip: Groovy adds hundreds of useful methods to standard Java classes through the GDK (Groovy Development Kit). Methods like capitalize(), padLeft(), toInteger(), and collect() are all GDK additions.

How Groovy Runs Your Code Under the Hood

Ever wondered what happens when you run groovy hello.groovy? Here’s the internal process:

  1. Parsing: Groovy reads your script and parses it into an Abstract Syntax Tree (AST)
  2. Compilation: The AST is compiled into JVM bytecode – your script becomes a Java class behind the scenes
  3. Wrapping: Script-level code gets wrapped in a run() method inside a class that extends groovy.lang.Script
  4. Execution: The JVM runs the compiled bytecode – same performance as Java at this point

So your simple println "Hello, World!" actually becomes something like this internally:

What Groovy Generates Internally

// Groovy generates something similar to this:
public class hello extends groovy.lang.Script {
    public Object run() {
        return this.println("Hello, World!");
    }

    public static void main(String[] args) {
        new hello(new Binding(args)).run();
    }
}

You write one line, Groovy handles the rest. That’s the beauty of it.

Groovy vs Java Hello World

Let’s put them side by side. Here’s the same program in Java and Groovy:

Java Version (HelloWorld.java)

Java Hello World

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

Groovy Version (hello.groovy)

Groovy Hello World

println "Hello, World!"

Same result. Java needs 5 lines with a class declaration, access modifiers, a static main method, and System.out.println(). Groovy does it in one line. And both produce identical bytecode on the JVM.

FeatureJavaGroovy
Lines of code51
Class requiredYesNo
Main method requiredYesNo
SemicolonsRequiredOptional
Compilation stepjavac + javagroovy (one command)
OutputHello, World!Hello, World!

Edge Cases and Best Practices

Edge Case 1: Print vs Println

print vs println

print "Hello, "
print "World!"
println()  // adds newline
println "---"
println "Done"

Output

Hello, World!
---
Done

Use print when you want to stay on the same line, and println when you want a line break after. Learn more in our print vs println guide.

Edge Case 2: Single Quotes vs Double Quotes

Single vs Double Quotes

def name = "World"

println 'Hello, ${name}!'   // Single quotes - NO interpolation
println "Hello, ${name}!"   // Double quotes - interpolation works

Output

Hello, ${name}!
Hello, World!

This catches a lot of beginners. Single-quoted strings are plain Java strings – no interpolation. Double-quoted strings are GStrings and support ${} expressions.

Edge Case 3: Special Characters

Special Characters

println "Tab:\tHello"
println "Newline:\nWorld"
println "Quote: \"Hello\""
println "Backslash: \\"

Output

Tab:	Hello
Newline:
World
Quote: "Hello"
Backslash: \

Best Practices Summary

DO:

  • Use println instead of System.out.println()
  • Use double quotes when you need string interpolation
  • Use single quotes for plain strings without variables
  • Keep scripts simple – skip the class wrapper unless you need it

DON’T:

  • Write Java-style boilerplate when a simple script will do
  • Use single quotes and expect interpolation to work
  • Add unnecessary semicolons (they’re optional in Groovy)

Common Pitfalls

Pitfall 1: Forgetting That Single Quotes Don’t Interpolate

Problem:

Wrong Way

def user = "Alice"
println 'Welcome, ${user}!'  // Oops - literal output

Output

Welcome, ${user}!

Solution:

Correct Way

def user = "Alice"
println "Welcome, ${user}!"  // Double quotes for interpolation

Output

Welcome, Alice!

Pitfall 2: File Extension Matters

Your Groovy script file must end with .groovy. If you save it as hello.txt and try to run it with groovy hello.txt, Groovy will still run it – but IDEs and build tools won’t recognize it properly. Stick to the .groovy extension.

Pitfall 3: Java Path vs Groovy Path

Make sure GROOVY_HOME is set and groovy is in your PATH. If you get “groovy: command not found”, your installation isn’t in the system path. SDKMAN handles this automatically – another reason to use it.

Conclusion

We covered a lot of ground today – from the simplest possible Groovy hello world one-liner to multiline strings, closures, maps, file operations, and even what happens internally when Groovy runs your code.

The key thing to remember is this: Groovy lets you start simple and grow complex only when you need to. A one-line script is just as valid as a fully structured class. That’s the philosophy behind the language – don’t make the programmer write ceremony code that the compiler could figure out on its own.

The best way to learn is to open your terminal and try these examples yourself. Modify them, break them, fix them. Try printing your own name, creating a list of your favorite tools, or writing a greeting to a file. Every experiment teaches you something.

Summary

  • println "Hello, World!" is a complete Groovy program – no class or main method needed
  • Double-quoted strings support ${} interpolation; single-quoted strings don’t
  • All valid Java code runs in Groovy, but Groovy code is much more concise
  • Closures, lists, maps, and GDK methods make Groovy expressive and fun
  • Groovy scripts compile to JVM bytecode – same platform, less boilerplate

If you also work with build tools, CI/CD pipelines, or cloud CLIs, check out Command Playground to practice 105+ CLI tools directly in your browser — no install needed.

Up next: Setting Up Groovy IDE – IntelliJ IDEA, VS Code, and Eclipse

Frequently Asked Questions

How do I run a Groovy hello world program?

Save println "Hello, World!" in a file named hello.groovy, then run it from your terminal with groovy hello.groovy. You can also run it directly with groovy -e "println 'Hello, World!'" without creating a file.

Do I need to install Java before installing Groovy?

Yes, Groovy runs on the Java Virtual Machine (JVM), so you need Java 17 or higher installed. Download it from Adoptium or use your package manager. Groovy 5.x requires Java 17+ as the minimum version.

What is the difference between Groovy and Java hello world?

In Java, you need a class declaration, a public static void main method, and System.out.println(). In Groovy, you just write println "Hello, World!" – one line. Both compile to the same JVM bytecode, but Groovy eliminates the boilerplate.

Can I use Java code inside a Groovy script?

Yes, almost all Java code is valid Groovy code. You can write pure Java syntax in a .groovy file and it will compile and run. This makes migrating from Java to Groovy very easy – you can start with Java and gradually adopt Groovy features.

Is Groovy still used in 2026?

Absolutely. Groovy powers Gradle (the most popular Java build tool), Jenkins CI/CD pipelines, the Grails web framework, and is widely used for scripting and testing. Groovy 5.x is actively maintained under the Apache Software Foundation with regular releases.

Next in Series: Setting Up Groovy IDE – IntelliJ IDEA, VS Code, and Eclipse

Related Topics You Might Like:

This post is part of the Groovy & Grails Cookbook series on TechnoScripts.com

RahulAuthor posts

Avatar for Rahul

Rahul is a passionate IT professional who loves to sharing his knowledge with others and inspiring them to expand their technical knowledge. Rahul's current objective is to write informative and easy-to-understand articles to help people avoid day-to-day technical issues altogether. Follow Rahul's blog to stay informed on the latest trends in IT and gain insights into how to tackle complex technical issues. Whether you're a beginner or an expert in the field, Rahul's articles are sure to leave you feeling inspired and informed.

No comment

Leave a Reply

Your email address will not be published. Required fields are marked *