Groovy

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.

Download NOW!

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: Sign up
Tags

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.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button