Over a million developers have joined DZone.

Create a Groovy 1.5.7 Script in NetBeans IDE 6.5

DZone 's Guide to

Create a Groovy 1.5.7 Script in NetBeans IDE 6.5

· Java Zone ·
Free Resource

In this article we will write a little Groovy script using NetBeans 6.5. NetBeans has Groovy (and Grails) support. We will see how we can use the latest Groovy version, 1.5.7, to support our script.

Groovy support in NetBeans is coupled to Java projects. There is no special Groovy project or anything. So we start to create a new Java project. We go to File | New Project and select Java from the Categories and Java Application from the Projects:

We click the Next button and we can fill in a project name. For this example we fill in GroovyScripts. We can uncheck Create Main Class, because we only want this project to contains Groovy scripts. We can check or uncheck Set as Main Project depending if we want this project to be the main project for NetBeans:

We can now press the Finish button and NetBeans will create a new Java project for us. Now we can add a new Groovy script to the project. We go to File | New File and select Groovy from Categories and then select Groovy Script from the File Types:

We click on the Next button and we get a new dialog window. Here we can type the Class Name. We type curlrange for the class name. We must check Location is set to Source Packages:

Now we can press the Finish button. NetBeans creates a sample Groovy script file in our project. And NetBeans enables Groovy support for our Java project. Because we add a Groovy script file to the project NetBeans enables Groovy support automatically! The sample Groovy script file display a string on the output window when we run the file. We can run a Groovy script file via Run | Run File or the shortcut key Shift+F6 in Windows.

We replace the sample script code with the following line:

println "${new Date().format('dd MMMM yyyy')}"

We can run the Groovy script, but we get an error. NetBeans doesn't recognize the format method for a Date object. This is because NetBeans support Groovy 1.5.5 and the format method is added in Groovy 1.5.7. So we need to tell NetBeans to support Groovy 1.5.7.

We go to File | Project Properies (GroovyScripts) | Libraries. Here we see the default Groovy 1.5.5 library. We press the Remove button to delete this library from the project. Next we press the Add Library button. NetBeans opens a dialog window where we can select from a list of available libraries. We press the Create button to create a new library for Groovy 1.5.7. NetBeans opens a dialog where we type Groovy_1.5.7 (spaces are not allowed in library names, so we use an underscore):

We press the OK button and we get a new dialog window. Here we can add the groovy-all-1.5.7.jar file from the embedabble directory of the Groovy 1.5.7 installation directory. If we don't have Groovy 1.5.7 already on our computer we can download it from the Groovy website. Now we can close the dialog by pressing the OK button.

We are still in the Project Properties dialog window. We go to the Compile Test tab and replace the Groovy 1.5.5 library with the Groovy_1.5.7 library. We click the OK button to close the project properties. We are ready to run our Groovy script again and this time we don't get an error anymore, because the format method is recognized. (Of course we still need to change the URL in the script to a real URL in order to run the file.).


Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}