Playing with Groovy – Tutorial 2

Groovy Distribution

Groovy comes with following utilities

  • Groovy Intepreter
  • Groovy Compiler
  • Groovy Shell
  • Groovy Console

 

Groovy Intepreter

An Intepreter is something which executes the Source Code on the fly without generating Intermediate Files. Till now, we are running the Groovy programs with the help of Groovy Intepreter only. In Groovy distribution, the Intepreter is available in the form of groovy.bat. To execute a Groovy Program, simply type groovy followed by the path of the Groovy file. For example, the following command executes the Hello.Groovy file available in the src directory.

1 groovy src/Hello.Groovy

One thing to note about Groovy Intepreter is that, whenever a Groovy File is fed, the Groovy Parser will parse checking for any Syntax or Construct mismatch. Then the conversion of the Groovy Source File into Java Byte-code will take place, followed by the invocation of the JVM on the byte-code.

5.3) Groovy Compiler

Invoking the Groovy Intepreter against a Groovy File may be time consuming especially if the size of the Groovy File is large. In such cases, optimization of the execution of the Groovy Script Files should be taken into consideration. Execution of the script files can be optimized by using a Groovy Compiler. The two step process of executing a Groovy File using Groovy Compiler is mentioned as below,

  • Conversion of the Groovy File into a .class File using the Groovy Compiler
  • Running the Class File using the Java Intepreter

Suppose that we have a file called Hello.groovy then the following command can be used to convert the groovy file into a class file.

1 groovyc Hello.groovy –d classes

In the above command, groovyc is the name of the Groovy compiler operating on the File Hello.groovy. The option '-d' represents the directory for the Generated Java Class files.
Running the Generated Class File using the JVM mandates the classpath to point to groovy-all-1.1-BETA-1.jar which is located in the GROOVY_HOME\embeddable location. This Jar file contains groovy specified libraries which must be added to the classpath before running the Hello.class file.

1 set classpath=%classpath%;GROOVY_HOME\embeddable\groovy-all-1.1-BETA-1.jar;
2
3 java classes\Hello.class

5.4) Groovy Shell

The Groovy Shell provides the Command Line Environment for running Groovy Scripts. To start the Groovy shell, simply type groovysh (Groovy Shell) in the command prompt.

1 groovysh

After entering into the Groovy shell, statements can be entered and executed directly. For example, suppose we wish to add two numbers and tell print the result value, then the following command along with statements will help.

1 groovy> def number1 = 10
2 groovy> def number2 = 32
3 groovy> def number3 = number1 + number2
4 groovy> println "Addition of $number1 plus $number2 is $number3"
5 groovy> go
6 Addition of 10 plus 32 is 42

The 'go' command is used to execute the command containing the statements that were previously entered.

5.5) Groovy Console

The Groovy Console provides a Graphical User Interface for typing and executing Groovy Scripts. To invoke the Groovy console, type groovyConsole in the command prompt that will open the following window.
Command Statements can be typed into the upper text-field and can be executed by pressing the Ctrl+R key or Actions->Run Menu. Scripts can be saved by using Ctrl+S and the existing Script can be loaded and executed by using the combination of Ctrl+O and Ctrl+R.

– See more at: http://www.javabeat.net/introduction-to-groovy-scripting-language/#sthash.uIdTNtri.dpuf

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: