Enjoy using Groovy Shell

Keywords: Java shell Programming Windows

This is a post about Groovy Shell and how it can help you in your daily work (as long as you are a software developer). No matter what programming language or technology you use, you can benefit from Groovy Shell. The only real requirement is that you can write (and read) a small piece of Groovy code.

Introduction

I think the purpose of Groovy shell is best described by official documents:

Groovy Shell, aka. groovysh is a command-line application that can be easily accessed to evaluate groovy expressions, define classes, and run simple experiments.

The Groovy Shell is included in the distribution of the Groovy programming language and can be found in < Groovy Home > / bin. To start the Groovy Shell, simply run groovysh from the command line:

GROOVY_HOME\bin>groovysh
Groovy Shell (2.2.2, JVM: 1.7.0)
Type 'help' or '\h' for help.
--------------------------------------------------------------------
groovy:000>

Now you can run the Groovy command in the shell:

groovy:000> println("hu?")
hu?
===> null
groovy:000>

It supports variables and multiline statements:

groovy:000> foo = 42
===> 42
groovy:000> baz = {
groovy:001> return 42 * 2
groovy:002> }
===> groovysh_evaluate$_run_closure1@3c661f99
groovy:000> baz(foo)
===> 84
groovy:000>
  • (note that you must skip the def keyword to use variables and closures later)

Notes to Windows users

I can clearly recommend Console (2), which is a small wrapper for the clunky cmd window. It provides Tab support, better text selection, and other useful features.

Unfortunately, in some areas, including German, the Groovy 2.2.0 Shell has problems with the arrow keys on Windows 7/8. However, you can use CTRL-P and CTRL-N instead of UP and DOWN. As an alternative, you can use the old Groovy version of the shell (groovysh from Groovy 2.1.9 works).

So, can we use it?

The most obvious thing we can do is evaluate Groovy code. This is especially useful if you are working on an application that uses Groovy.

You may know that you can use the < operator to add elements to the list, but are not sure if it applies to maps? In this case, you can start a Google search or search in the document. Alternatively, you can type it into a Groovy Shell and see if it works:

groovy:000> [a:1] << [b:2]
===> {a=1, b=2}

Useful!

Are you not sure you can traverse enumeration values?

groovy:000> enum Day { Mo, Tu, We }
===> true
groovy:000> Day.each { println it }
Mo
Tu
We
===> class Day

This is a calculator!

Groovy Shell can be used for simple mathematical calculations:

groovy:000> 40 + 2
===> 42
groovy:000>
groovy:000> 123456789123456789 * 123456789123456789123456789
===> 15241578780673678530864199515622620750190521
groovy:000>
groovy:000> 2 ** 1024
===> 179769313486231590772930519078902473361797697894230657273430081157732675805500963132708477322407536021120113879871393357658789768814416622492847430639474124377767893424865485276302219601246094119453082952085005768838150682342462881473913110540827237163350510684586298239947245938479716304835356329624224137216
groovy:000>

As you can see, groovy does a good job of handling numbers that can overflow other programming languages. Groovy uses BigInteger and BigDecimal for these calculations. By the way, you can quickly verify yourself:

groovy:000> (2 ** 1024).getClass()
===> class java.math.BigInteger

More likely

Maybe you need the content of a web page? Groovy makes it easy to:

groovy:000> "http://groovy.codehaus.org".toURL().text<font></font>
===> <!DOCTYPE html><font></font>
<html><font></font>
<head><font></font>
    <meta charset="utf-8"/><font></font>
    <meta http-equiv="content-type" content="text/html; charset=utf-8"/><font></font>
    <meta name="description" content="Groovy Wiki"/><font></font>
    ...

Maybe for some reason, you just want the < meta > tag?

groovy:000> "http://groovy.codehaus.org".toURL().eachLine { if (it.contains('<meta')) println it }
    <meta charset="utf-8"/>
    <meta http-equiv="content-type" content="text/html; charset=utf-8"/>
    <meta name="description" content="Groovy Wiki"/>
    <meta name="keywords"
    <meta name="author" content="Codehaus Groovy Community"/>
===> null

I'm sure you're in a situation where you need a url encoded version of some text:

groovy:000> URLEncoder.encode("foo=bar")
===> foo%3Dbar

Of course, you don't need to remember the exact class and method names. Just type the first few characters and press Tab for possible options:

groovy:000> URL
URL                       URLClassLoader            URLConnection             URLDecoder                URLEncoder
URLStreamHandler          URLStreamHandlerFactory

It also applies to methods:

groovy:000> URLEncoder.e
each(            eachWithIndex(   encode(          every(           every()

conclusion

Before switching to Groovy Shell, I used Python Shell for almost the same reason (even if I didn't use Python at all). In the past year, I have used a lot of Groovy, and soon I found that Groovy Web Console is a very valuable tool for testing and prototyping. For me, Groovy Shell replaces these two tools. Obviously, this is a development tool I don't want to miss.

Selected technical articles

Selected non-technical articles

Posted by mrdave on Fri, 22 Nov 2019 02:04:55 -0800