Christoph Hartmann on March 31st, 2008

Acording to the official Groovy web site Groovy is “an agile dynamic language for the Java Platform with many features that are inspired by languages like Python, Ruby and Smalltalk, making them available to Java developers using a Java-like syntax.”

Besides all marketing I used Groovy for quite some time and I think it’s a great scripting language for Java. If you are new to Groovy you should read the book: “Dierk Koenig: Groovy in Action”. After 2 days of working with Groovy you will become a real Groovy developer. And the best about Groovy is that your old Java code can be reused.

Get the stuff workig

Although Groovy is available as a separate package here, I would recommend to use Groovy in combination with Eclipse. This works easily on Mac and Windows and fits to Java programmers most due to the familiar Eclipse environment. Compared to Eclipse the Groovy scripting console shipped with the Groovy libraries called “Groovy Console” is only useful to write simple Code snippets. If your project becomes bigger the Eclipse environment is more powerful.

After a properly set up of Eclipse, open it and go to Help -> Software Updates -> Find and Install -> Search For New Features. Now add a New Remote Site… and insert “Grovvy Plugin” and “http://dist.codehaus.org/groovy/distributions/update/” as URL. Confirm the data with OK. Now check the newly added Site and press Finish.

The following screen will ask you to Select the Features to Install. Just select the Groovy check box and press Next. Finally accept the license agreement and press Next. Confirm the default location with Finish. Now the plugin download starts.

After the Groovy plugin installation, it’s easy to start with Groovy. At first you create a new Java project and add a new Groovy class. Afterwards Eclipse asks you to add the Groovy runtime to the Java project and confirm this question with Yes. Within this project you can use Java and Groovy classes interchangeable. This is the best thing about Groovy runtime because it is a simple Java jar file.

class HelloWorld {
   static void main(args) {
 
   }
}

Add println “Hello World” and open the Run Dialog. Currently the dialog does not find Groovy main classes. Therefore you have to add it manually. After a click on Run the Console outputs: Hello World

To sum up: it’s so simple to run Groovy code.

Tags: ,

Leave a Reply