Hello World in Groovy

Our first program will print the classic “hello world” message. Here’s the full source code.

println "hello world"

To run the program, simply execute the Groovy script.

$ groovy hello-world.groovy
hello world

Sometimes we’ll want to package our scripts into an executable JAR file. Here’s how to do it in Groovy:

First, create the script:

// hello-world.groovy
println "hello world"

Then, compile the script and package it into a JAR file using the following steps:

// Compile the script
$ groovyc hello-world.groovy

// Package into a JAR file
$ echo 'Main-Class: hello_world' > Manifest.txt
$ jar cfm hello-world.jar Manifest.txt hello_world.class
$ ls
hello-world.groovy   hello_world.class    hello-world.jar

We can then execute the JAR file directly using the java -jar command.

$ java -jar hello-world.jar
hello world

Now that we can run and build basic Groovy programs, let’s learn more about the language.