Groovy Console Example
1. Introduction
In this tutorial, I will show you how to use Groovy Console to run your Groovy scripts and also give you some details about Groovy Console itself. Actually, there are several ways to run Groovy scripts. You can run it on your favourite IDE, you can run it from command line, or you can use Groovy Console to run your scripts.
Groovy Console is a Swing GUI for perform several operations to your Groovy scripts. You may want to use Groovy Console to write and run Groovy scripts instead of IDE or command line. Let’s see features together.
2. Action Buttons
In Groovy Console, there are lots of actions that you can see below.
Most of them are the actions that you may already know, but you can have a look at below to refresh your knowledge.
Action 1
is for creating new script to write your code in a fresh areaAction 2
is for Opening fileAction 3
is for Saving your code as projectAction 4
andAction 5
are for Undo and Redo respectivelyAction 6, 7, 8
are for Cut, Copy, Paste respectivelyAction 9
is for looking up something with in your codeAction 10
is for Replacing text with another text within your code- In Groovy Console, your executing actions are kept historically. You can go forward and backward along running history with
Action 11
andAction 12
Action 13
is for Running your current scriptAction 14
is for Interrupting current running script
3. Quick Start
I assume you have already installed Groovy on your computer and added GROOVY_HOME/bin
to your PATH
to run groovy binaries. You can open Groovy console by executing groovyconsole
command. You will see Groovy Console GUI like following.
There are two area in the GUI: Input Area, Output Area. You can write your code in Input Area and click Run button at the top right of the GUI (or Ctrl + Enter), and see result in Output Area as below.
As you can see, there are only codes in the output area, because results are printed in the command line console where you started Groovy Console from. If you want to see results also in the output area, you need to enable Capture Standard Output like below.
You can also open Groovy script file by using File > Open menu and the script will loaded in the input area. Then you can run loaded script.
4. Run Selection
Additionally, you can run only selected portion of the code in Input Area. If you select a section in the Input Area and click Run Selection (or Ctrl + Shift + R) in the Script menu, only the selected code will be executed as below.
5. Interruption
You can interrupt current running script in Groovy Console after enabling interruption by selecting Script > Allow Interruption menu. What I mean by interruption here is interrupting current thread in running script. For example, in order to interrupt following script.
try { while(1) { println "something" Thread.currentThread().sleep(500) } } catch(InterruptedException ex) { println "Interrupted from Groovy Console" }
You can first Run script and then you can click Interrupt button on the right side of the Run button. You will see an output like below.
6. Embedding Console
If you want to use Groovy Console within your Java or Groovy application, you can easily embed Groovy Console by using following example.
package main.java.javacodegeeks.groovyconsole import groovy.ui.Console; class GroovyConsole { static main(args) { Console console = new Console(); console.setVariable("name", "John"); console.setVariable("surname", "Doe"); console.run(); } }
When you execute above script, you will see Groovy Console opened and variables name
and surname
will be predefined. And you will be able to use that variables. You can see an example below.
As you can see, we are able to use $name
and $surname
even if we did not explicitly defined them.
7. Conclusion
Groovy Console is an alternative to Groovy Sh or your favourite IDE. You can quickly write your code and execute to see what is going on with entire Groovy code. Also, you can embed Groovy Console to your application to define some variables to console and run it with that variables.
You can download the full source code of the project here: GroovyConsoleExample