Groovy 🎷 Cheat Sheet - 01 Say "Hello" from Groovy
Tags: groovy, java, beginners
Published: 2024-03-07 10:43:03

**"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](https://en.wikipedia.org/wiki/Java_virtual_machine) languages universe" again. Yes, language(s), plural! Java isn't the only language that uses the JVM. I previously used [Scala](https://www.scala-lang.org/), which is a JVM language, to use [Apache Spark](https://spark.apache.org/) for Data Engineering workloads, but this is for another post 😉.

This time, I visited [Groovy](https://groovy-lang.org/). 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](#my-first-impressions-about-groovy)
- [Setting up your "Groovy" environment](#setting-up-your-groovy-environment)
    - [Installation](#installation)
    - [Groovy tools](#groovy-tools)
    - [IDEs buffet](#ides-buffet)
    - [Groovy "Hello, World"](#groovy-hello-world)
- [\[BONUS\] Running Groovy in a Jupyter Notebook](#bonus-running-groovy-in-a-jupyter-notebook)
- [Conclusion](#conclusion)

### 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:
```java
public class SayHello {
    public static void main(String args[]) {
        System.out.println("Hello, from JAVA");
    }
}
```

![java](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ni8wab43a4gqw64gi3fl.gif)
> 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](https://www.rust-lang.org/) in this [LinkedIn post](https://www.linkedin.com/posts/fady-abdelmassih_just-a-little-test-to-see-how-groovy-compares-activity-7164699667891523584-czx7?utm_source=share&utm_medium=member_desktop) 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](https://en.wikipedia.org/wiki/Integrated_development_environment). 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](https://en.wikipedia.org/wiki/Java_Development_Kit) (Java Development Kit)
- The Groovy jar files.

The manual setup process contains:
1. Download and install you favorite JDK. (I use [OpenJDK](https://openjdk.org/) 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](https://groovy.apache.org/download.html) 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](https://sdkman.io/). 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](https://learn.microsoft.com/en-us/windows/wsl/install) or [Cygwin](https://cygwin.com/) as the official page suggests.
It is really simple to use `sdkman`. after a successful [installation](https://sdkman.io/install), just type those commands into your \*nix shell:
```sh
sdk install java
```
```sh
sdk install groovy
```

And that's it. You are good to go 😃  

For a complete usage guide, check its [official page](https://sdkman.io/usage).

You can check if you have a correct environment istallation by typing those commands into your system's shell:
```sh
java --version
```
```sh
groovy --version
```

![successful installation](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kdi2issd50uorurlqxxu.png)

#### Groovy tools
Groovy installation comes with 3 main cli-ish tools:
- `groovy`: The Groovy command line processor

![groovy cli](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lqyg6d6z2jbox13lnofa.gif)
- `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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xqicsqwgfyqv4dva2jqy.gif)

#### 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](https://code.visualstudio.com/). But for Groovy, it didn't offer the right extensions.

Next, I've tried [Apache Netbeans](https://netbeans.apache.org/front/main/). 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](https://www.jetbrains.com/idea/) (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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/t84dapbhagwgoc265c49.gif)

#### 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`:
```sh
echo "println 'Hello, World'" > helloWorld.groovy
groovy helloWorld
```

![Hello world cli](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mjidy0kfhdgbdu6nyecq.png)

And another "Hello, World" from the `groovyConsole`:

![console hello](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/x06m94u4gzxd5xc339gv.gif)


Yup, that's it! Compare that to the Java class I wrote earlier!
### \[BONUS\] Running Groovy in a Jupyter Notebook

[Jupyter Notebooks](https://jupyter.org/) 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](http://beakerx.com/)!

The easiest way to try it is to use its `Docker` container as follows (you must have docker installed in your system):
```sh
docker run -p 8888:8888 beakerx/beakerx
# Image pulling will take sometime if it's the first time you run it.
```
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9ezk1hfofcp3hu84kv3r.gif)

### 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!