Home » JVM Languages » Groovy » Groovy Console Example

About Huseyin Babal

Huseyin Babal
Huseyin Babal has deep experience in Full Stack Development since 2007. He is mainly developing applications with JAVA, Spring, PHP, NodeJS, AngularJS. He is also interested in DevOps Engineering since 2013 and using AWS, Heroku for Cloud deployment and playing with Docker and Consul for implementing infinite scalable systems. He likes to share his experience in public conferences and perform advanced workshops about Full Stack Development and Devops. He is the author of NodeJS in Action course in Udemy.

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.

Action Buttons

Action Buttons

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 area
  • Action 2 is for Opening file
  • Action 3 is for Saving your code as project
  • Action 4 and Action 5 are for Undo and Redo respectively
  • Action 6, 7, 8 are for Cut, Copy, Paste respectively
  • Action 9 is for looking up something with in your code
  • Action 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 and Action 12
  • Action 13 is for Running your current script
  • Action 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.

Groovy Console Start

Groovy Console Start

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.

Groovy Console Input and Output Area

Groovy Console Input and Output Area

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.

Enable Output Capture

Enable Output Capture

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.

Run Selection

Run Selection

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.

Interruption

Interruption

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.

Groovy Console Embed Example

Groovy Console Embed Example

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.

Download
You can download the full source code of the project here: GroovyConsoleExample
(No Ratings Yet)
Start the discussion Views Tweet it!

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!

 

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

 

and many more ....

 

Receive Java & Developer job alerts in your Area

 

Leave a Reply

avatar
  Subscribe  
Notify of