Loading...

December 15, 2008

Create a Groovy 1.5.7 script in NetBeans 6.5

In a previous post we saw how we can invoke a external process and how to use date ranges in Groovy 1.5.7. In this post we will write this little 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 copy the code from the previous post in the code editor of NetBeans, replacing the sample code NetBeans generated. 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.).