The Groovy distribution has another option for developers who are more familiar with a graphical interface and want to try out code snippets. The tool in question is groovyConsole
, which is very similar to groovysh
(see the Using groovysh to try out Groovy commands recipe) except that it provides a GUI with syntax highlighting and basic code editor features.
In this recipe, we are going to cover basic GroovyConsole usage scenarios.
Open your shell and let's dive in:
groovyConsole
command in your command line:The GroovyConsole screen is divided into two logical parts. The top area is meant for script editing, and the bottom area displays the script execution output when a script is launched.
groovysh
shell; that is, first the executed script is printed after the groovy> marker followed by the printed output and a result value, if any:groovy.lang.Script
class that is used to execute all the code in the console). If you change that code to reuse the previous field value (for example, +=
), you will actually notice that the value changes every time the code gets executed:groovysh
, if you define a variable using the def
keyword, then you can't reuse the previous value. The def
keyword is used to define locally scoped variables and in this case, it's bound only to single execution of the script:It is possible to increase or decrease the font size and configure the way the output panel displays information. For instance, you can disable the script output or clear the output screen at each run.
You can run or abort your script execution, force script compilation, and add additional libraries to the class path. Also, you can Clear Script Context, which will lead to clearing all the script's accumulated properties (for example, test).
The GroovyConsole tool comes with a handy Object Browser that graphically shows the fields and methods available for a given class.
Press Ctrl + I (CMD + I on a Mac) to launch the browser and display the last evaluated expression. The following simple class would appear like in the following screenshot:
class Person { String name String lastName } p = new Person()
By pressing Ctrl + J (CMD + J on a Mac), we can display another view of the Object Browser, this time focused on the variables visible from within the script.