Create a Groovy 1.5.7 Script in NetBeans IDE 6.5

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.).

References
0

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Comments

EricElz replied on Wed, 2008/12/17 - 3:33pm

Should the code complete work also for the .format method after i changed the libraries?..it doesn't for me

Hubert Klein Ikkink replied on Thu, 2008/12/18 - 1:42am in response to: EricElz

Hi, did you remove the "old" Groovy 1.5.5 library from the Libraries section? If not NetBeans loads the old library first so you cannot use the Date.format method. If you want I can take a look at your NetBeans project to see if I can find the problem. Just send me a message via my profile page (http://netbeans.dzone.com/users/mrhaki).

EricElz replied on Thu, 2008/12/18 - 9:25am

Hi!

 

Yes i removed the old library.
The project itselfs runs ok..and i do get output..i just wondered why the code completion doesnt list the format method when i do "new Date()." and wait

I would expect to see the format method in this popup..right ?

Hubert Klein Ikkink replied on Fri, 2008/12/19 - 3:16am in response to: EricElz

Hi, I was looking for a way to run Groovy 1.5.7 scripts in NetBeans. I hadn't tried the code completion yet, but  you are right the code completion doesn't pick up the 1.5.7 methods. I haven't found a way to achieve this yet, but I keep looking.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.