DEV Community

Cover image for Groovy 🎷 Cheat Sheet - 01 Say "Hello" from Groovy
Fady GA 😎
Fady GA 😎

Posted on

Groovy 🎷 Cheat Sheet - 01 Say "Hello" from Groovy

"Java is great, but I don't like it!"

This was my first impression when I started to learn the language. Maybe if my first language I learnt was C++ instead of Python, I'd have another opinion, but who knows πŸ€·β€β™‚οΈ.

Recently I had to revisit the "JVM languages universe" again. Yes, language(s), plural! Java isn't the only language that uses the JVM. I previously used Scala, which is a JVM language, to use Apache Spark for Data Engineering workloads, but this is for another post πŸ˜‰.

This time, I visited Groovy. Given my biased first impression about Java and the JVM, I wasn't too exited! But I couldn't have been more wrong! 😁

Table of Contents:

My first impressions about Groovy

I don't consider myself a professional software developer. I'm merely a data person who mainly uses Python (among other things) in my daily work and trying my best to follow the principals of Software Engineering in whatever I build. That was a mouthful πŸ˜‚

When I started to pick up Groovy, I instantly noticed the similarities between it and Python despite being a JVM language. I could see that Groovy was built to supplement Java with "scripting" capabilities. When I started to dig deeper into the learning resources, I found out that Groovy wasn't meant to replace Java! But to supercharge your Java codebase and to make the "Ceremonious boiler plate code" redundant (a phrase I stole from Dan Vega 😁) while still using the JVM. I also read that "Groovy is Python for Java" πŸ˜‰

To demonstrate this, it took me around 2 minutes to create a very simple class that just print "Hello" using java with this code. Now imagine creating a whole app like this! We will see how it is significantly faster (and shorter) with Groovy:

public class SayHello {
    public static void main(String args[]) {
        System.out.println("Hello, from JAVA");
    }
}
Enter fullscreen mode Exit fullscreen mode

java

I'm a bit of a slow typer and other super devs out there might do the same in a heartbeat, but you get the idea πŸ˜‰

But that said, - and again I might be a bit biased - Groovy is too slow for me! I compared it to Rust in this LinkedIn post and it was waaaaay slow. Keep in mind that subjectively comparing programming languages might be a tricky business. But at the end, it will be up to your use case/project to prefer a language over the other.

Now, a trick I picked up earlier, is that

If you need to learn something, teach it!

The logic behind that, is whenever you need to teach anything, you are either an expert in that topic or not. When you aren't an expert, you will need to excel at that topic in order to offer credible and high value knowledge to the ones you teach.

So, I want to learn Groovy, then I'll teach it! I'll post a short series (3-4 posts at most) about Groovy. It won't be an in-depth series, it will only give you what you need to get up and running with Groovy. A Cheat Sheet if you will...

In this one, I'll start with the basics, enjoy 😎

Setting up your "Groovy" environment

As with any programming language, your development environment is basically installing the language itself and an IDE. In the following section we will see how to setup our environment and the different options that we have to do so.

Installation

As Groovy is a JVM language, you will need 2 things to start coding with it:

  • The JDK (Java Development Kit)
  • The Groovy jar files.

The manual setup process contains:

  1. Download and install you favorite JDK. (I use OpenJDK 17. You can use the 21 stable version, it won't really matter at this stage which JDK provider you choose (yes, there are many) and the version as long as it's relatively new 😁)
  2. Depending on your OS, make sure that there is a JAVA_HOME env var pointing to the bin directory insider your JDK installation directory.
  3. Download and install the latest Groovy version either using the installers or the binary zip files. It all falls down to your personal preference.
  4. Like with the JDK, make sure that you have a GROOVY_HOME env var pointing to the bin directory in your Groovy installation directory.

If you survive all of this, you should be more or less good to go! πŸ˜‚

Alternatively, you can use sdkman. A great tool to install your Software Development Kit. The downside is that it only works on *nix systems. So for Widnows users, you will have to use WSL or Cygwin as the official page suggests.
It is really simple to use sdkman. after a successful installation, just type those commands into your *nix shell:

sdk install java
Enter fullscreen mode Exit fullscreen mode
sdk install groovy
Enter fullscreen mode Exit fullscreen mode

And that's it. You are good to go πŸ˜ƒ

For a complete usage guide, check its official page.

You can check if you have a correct environment istallation by typing those commands into your system's shell:

java --version
Enter fullscreen mode Exit fullscreen mode
groovy --version
Enter fullscreen mode Exit fullscreen mode

successful installation

Groovy tools

Groovy installation comes with 3 main cli-ish tools:

  • groovy: The Groovy command line processor

groovy cli

  • groovyc: The Groovy compiler (I rarely use that, at least for now)
  • groovyConsole: The Groovy REPL (Read Evaluate Print Loop). In other words, a minimalist IDE. While learning, the groovyConsole will be all what you need. It's very basic and can't work within WSL. groovyConsole

IDEs buffet

While the groovyConsole will satisfy all of our learning needs, if you ever wanted to go the next "groovy" level, you will most probably need an IDE. IDEs comes with all the bells and whistles that should make you more productive and grooving 😁.

I personally prefer VS Code. But for Groovy, it didn't offer the right extensions.

Next, I've tried Apache Netbeans. It felt like it's built for Java rather than Groovy. I couldn't make its basic "IntelliSense" (code auto-completion) feature correctly work for Groovy so I ended up abandoning it.

IntelliJ (look for the community edition) on the other hand offered great out-of-box Groovy support including IntelliSence, building, and running features. So, I sticked with it 😊

intellij

Groovy "Hello, World"

Now this won't be an introductory programming language post without the classic "Hello, World" example, right? πŸ˜‰

I'll use the most basic commands using bash:

echo "println 'Hello, World'" > helloWorld.groovy
groovy helloWorld
Enter fullscreen mode Exit fullscreen mode

Hello world cli

And another "Hello, World" from the groovyConsole:

console hello

Yup, that's it! Compare that to the Java class I wrote earlier!

[BONUS] Running Groovy in a Jupyter Notebook

Jupyter Notebooks is very popular among data people specially Python users. So, I tried to find a way to run the Groovy kernel inside a Jupyter Notebook, and to my surprise, I found a way, BeakerX!

The easiest way to try it is to use its Docker container as follows (you must have docker installed in your system):

docker run -p 8888:8888 beakerx/beakerx
# Image pulling will take sometime if it's the first time you run it.
Enter fullscreen mode Exit fullscreen mode

The open up the url that you will be presented with and you will be good to go. (you will have to replace the funky server name with localhost for it to work, or at least this is how it worked for me)

This is another "Hello, World" example using Jupyter with a Groovy kernel:

Jupyter

Conclusion

Groovy is very close to Python that runs in the JVM universe. It's not meant to replace Java, but to supplement it. Groovy already comes with all the tools you need to get started. In this one, I didn't go into Groovy language specifics. I'll do that in later posts. See you then!

Top comments (0)