How to import / evaluate Groovy scripts within groovy

By Turjo, Gaea News Network
Tuesday, October 26, 2010

If you are bored with your same old programming language, then help yourself with a little “Groovy” touch. Groovy is an object-oriented programming language which can be used as a scripting language for the Java Platform. The brainchild of superstar developers James Strachan and Bob McWhirter, Groovy is an agile development language that is based entirely on the Java programming APIs. It is dynamically compiled to Java Virtual Machine bytecode and interoperates with other Java code and libraries.

Although Java and Groovy, both are very simillar in nature, Groovy code can be more compact. It is simply because it does not require all the elements that Java requires. Unlike Java, a Groovy source code file can be executed as a script, if it contains code outside any class definition or it is a class with a main method.

One of the most exciting and powerful features found in Groovy is its support for closures. Closures are first class objects that are similar to anonymous inner classes found in the Java language. But unlike anonymous classes in Java, closures can be named and also reused. And, most important and true to Groovy, closures are infinitely more flexible than anonymous inner classes! Moreover, closures can also accept parameters. Any variables and closures in the file are available to the calling script.

A closure can be treated like a regular method within groovy. But, there is another simple way. We can static import (like java) to make all the static methods available within the calling script or a regular import can also be done. And perhaps the most simpliest way of invoking a static methods in a groovy script within the same directory is by writing abc.methodName(), where abc is the name of script and methodName is the static method defined within the script.

In Groovy, an object’s type is discovered dynamically at runtime, which greatly reduces the amount of code we have to write.

Groovy nicely blends some of the most useful features of Ruby, Python, and Smalltalk, while conserving a core syntax based on the Java language. For developers familiar with the Java platform, Groovy provides a simpler alternative with almost no ramp-up time. For developers new to the Java platform, it may act as an easy entry point to the more extensive syntax and requirements of the Java language. It can be safely said that Groovy is not a replacement for the Java language but an alternative to it.

YOUR VIEW POINT
NAME : (REQUIRED)
MAIL : (REQUIRED)
will not be displayed
WEBSITE : (OPTIONAL)
YOUR
COMMENT :