GroovyScriptingUsingNB65

Create a Groovy 1.5.7 script in NetBeans 6.5

Contributed by: Hubert A Klein Ikkink


Contents

Introduction


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.

Getting Started


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.

Create Groovy Project

We go to File | New Project and select Java from the Categories and Java Application from the Projects:

File:groovy-project_GroovyScriptingUsingNB65.png

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, that is up to ourselves:

File:groovy-project2_GroovyScriptingUsingNB65.png

Create Groovy Script

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:

File:groovy-project3_GroovyScriptingUsingNB65.png

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:

File:groovy-project4_GroovyScriptingUsingNB65.png

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.

NOTE: 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.

Run Groovy Script

Now, we can run a Groovy script file via Run | Run File or the shortcut key Shift+F6 in Windows.

Making Code Changes


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.

Customize Groovy Project


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

File:groovy-project7_GroovyScriptingUsingNB65.png

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.

File:groovy-project8_GroovyScriptingUsingNB65.png

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.


Reposted from this blog entry: http://mrhaki.blogspot.com/2008/12/create-groovy-script-in-netbeans-65-and.html


Not logged in. Log in, Register

By use of this website, you agree to the NetBeans Policies and Terms of Use. © 2012, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo