3 Groovy Tools for Writing Groovy Scripts

3 Groovy Tools for Writing Groovy Scripts

There are various tools offered by Groovy and today we are going to look at the 3 ways for writing Groovy scripts. Each method have their own purpose, pros and cons and based on that you can identify which one better suits you. We are going to go through each of the methods and see how it works.

Method 1 – Using Groovy Shell – groovysh:

The first method is to write the groovy scripts via the Groovy Shell. You just need to execute the following command in the shell terminal:

groovysh

This will open up the Groovy Shell prompt as shown below:

Groovy Shell Prompt

Here you can write your small expressions and Groovy code to test and experiment with. The Groovy shell is not suitable for writing large class files and it is mainly suitable only for writing small scripts which we would like to test and experiment the output. Hence writing groovy scripts with Groovy shell is advisable only for small scripts and expressions for testing purposes. To learn more about writing groovy scripts using the Groovy shell, you can refer our post on Using the Groovy Shell – groovysh.

Method 2 – Using Groovy Compiler – groovyc:

Groovy compiler is a tool offered by the Groovy that converts the Groovy code into Byte code which are saved in the class files with “.class” extension. You can write your Groovy code in a file and save it using the “.groovy” extension. Then you will need to execute the groovyc command on the file similar to the following:

groovyc FileName.groovy

This will generate a class file called as:

FileName.class

If you open up this .class file in any editor it will be full of byte code like shown below:

Groovy Class File Byte Code

In order to view the output of the code, you will need to execute the below command in the shell terminal:

groovy FileName.class

Note that we are executing the groovy command on the “.class” file and not the “.groovy” file. In order to explore more in using the Groovy compiler, you can refer Using Groovy Compiler – groovyc to Execute Groovy Files post.

Method 3 – Using Groovy Console – groovyConsole:

This is by far the most preferred and easy to use option to write Groovy scripts. The Groovy Console is another tool offered by Groovy to write groovy scripts in an easy to use Graphical User Interface (GUI). On the shell terminal just type the following command to open the Groovy Console interface:

groovyConsole

Executing this will open the Groovy Console interface:

Here you can see that we write the Groovy script in the upper pane and the output is displayed in the lower pane and in order to view the output of the Groovy code, you will need to press Ctrl + R. To get to know more about the Groovy Console, you can view Using groovyConsole for Writing Groovy Scripts.

Thus we just went through each of the methods for writing groovy scripts and we can conclude that learning purpose, the groovyConsole would be the best option as it generates output within a single window in a easy to use GUI. For writing bigger applications it is best to write the code using the good editor and use the Groovy compiler to compile the files all at once.

Related Posts