If you’re a Java developer looking to learn Groovy, you’re in the right place. This Groovy tutorial for Java developers starts at the very beginning a Hello World program and shows just how dramatically Groovy cuts through Java’s boilerplate. Groovy is a JVM language designed to reduce boilerplate code. It was created back in 2003 by James Strachan, at a time when Java was already known for being verbose. And let’s be honest no matter how much you love Java, it is verbose. Just look at what it takes to print “Hello World”:

class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World");
}
}
println "Hello, World!"
Quite a contrast, right? Under the hood, Groovy does the same thing — it just takes care of a lot of the boilerplate for you automatically. Here’s what it did behind the scenes:
- The class wrapper is optional — not everything needs to live inside a class. Groovy wraps your script in one automatically at compile/runtime.
- Parentheses are optional for method calls, which gives the code a more conversational, almost domain-specific language feel rather than traditional computer code.
- Several common Java packages — including
java.lang,java.util,java.io, and others — are automatically imported by Groovy in the background, so you don’t have to import them manually.
How to Run a Groovy Script (Groovy Tutorial Step 1)
One of the biggest advantages of this Groovy tutorial for Java developers is seeing how quickly you can get running — no compilation step needed. Just call:
groovy HelloWorld.groovy
PS D:\Projects\groovylabs\groovylabs_hello_world> groovy -version Groovy Version: 5.0.3 JVM: 21.0.9 Vendor: Oracle Corporation OS: Windows 11 PS D:\Projects\groovylabs\groovylabs_hello_world> groovy HelloWorld.groovy Hello World! PS D:\Projects\groovylabs\groovylabs_hello_world>
If you haven’t installed Groovy yet, check out the official Groovy installation guide — it covers Windows, macOS, and Linux. You’ll also find the official Groovy documentation helpful as you progress beyond Hello World.
Compiling Groovy to JVM Bytecode — Java Developers Will Feel Right at Home
In the section above, we ran the Groovy script directly without compiling it first. Now let’s compile it and run it using the JVM — after all, it’s a JVM-based language!
groovyc HelloWorld.groovy
This will generate a HelloWorld.class file:
Mode LastWriteTime Length Name ---- ------------- ------ ---- d---- 2/15/2026 10:04 PM .history -a--- 2/15/2026 10:05 PM 1792 HelloWorld.class -a--- 2/15/2026 10:04 PM 22 HelloWorld.groovy
Now let’s try running it with Java. Note: pass the class name, not the filename — drop the .class extension:
java HelloWorld
Error: Could not find or load main class HelloWorld Caused by: java.lang.NoClassDefFoundError: groovy/lang/Script
This error happens because the JVM can’t find the Groovy runtime. We need to include the Groovy SDK on the classpath. The convention is to set a GROOVY_HOME environment variable, and then run it like this:
java -cp "$PWD;$env:GROOVY_HOME\lib\*" HelloWorld
What’s Inside the Groovy-Generated Class File?
Let’s peek inside the .class file that groovyc generated. You can decompile it using a tool like IntelliJ IDEA’s built-in FernFlower decompiler:
// Source code is decompiled from a .class file using FernFlower decompiler (from IntelliJ IDEA).
import groovy.lang.Binding;
import groovy.lang.Script;
import groovy.transform.Generated;
import org.codehaus.groovy.runtime.InvokerHelper;
public class HelloWorld extends Script {
public HelloWorld() {
}
public HelloWorld(Binding context) {
super(context);
}
@Generated
public static void main(String... args) {
InvokerHelper.class.invoke<invokedynamic>(InvokerHelper.class, HelloWorld.class, args);
}
@Generated
public Object run() {
return this.invoke<invokedynamic>(this, "Hello World!");
}
}
See what happened? Groovy generated a full class for us — it extended Script, added a main method, and handled all the wiring. All that heavy lifting you’d normally do in Java? Groovy just did it for you automatically. This is exactly what makes Groovy such a powerful tool for Java developers — you get the full JVM ecosystem with a fraction of the code.
Ready to go deeper? Check out our next post on Groovy data types and variables for Java developers — or if you want to explore what else is possible on the JVM.
Repo link : srnyapathi/groovylabs_hello_world



