X

Geertjan's Blog

  • October 17, 2007

Compiling and Running Groovy from Java in NetBeans IDE

Geertjan Wielenga
Product Manager
Something I learned today at the Grails eXchange here in London. Without any plugins at all, you can already call Groovy scripts from Java code in NetBeans IDE, as outlined below.

  1. Start by creating a small script called "HelloWorld.groovy", using the "Empty File" template to create the file, with this content:
    package demojavaapplication
    class HelloWorld {
    static void main(args) {
    println "Hello from Groovy!"
    }
    }

    Now, we will set the project up so that the above will be called from a Java class in our application.

  2. First, here's an Ant script, added to my build.xml, that runs the Groovy compiler:
    <taskdef name="groovyc" 
    classpath="lib/groovy-all-1.0.jar"
    classname="org.codehaus.groovy.ant.Groovyc"/>
    <target name="-pre-compile">
    <groovyc srcdir="${src.dir}" destdir="groovy">
    <classpath path="lib/groovy-all-1.0.jar"/>
    </groovyc>
    </target>

  3. Now, to use the above Ant script, do the following:

    • Right-click the Libraries node and add groovy-all-1.0.jar from the embeddable folder in the Groovy distribution. Now create a folder called "lib", in the Files window, and put the JAR there.

    • In the Files window, create a folder called groovy and add that folder to the Libraries node, which puts it on the classpath.

    You now have set "groovy" as your destination directory for the Groovy compiler. You have added the JAR that provides the Groovy compiler. You have added the "groovy" folder to the compile time classpath, so its content is compiled, prior to compilation of the Java classes.

  4. When you build your application, you should now see this, while taking note of the fact that your Groovy script is now a Java class:

  5. Since your Groovy script is now a Java class, and the folder that contains it is on the compile time classpath, you can call it from a Java source file:

    public class Main {
    public static void main(String[] args) {
    HelloWorld hello = new HelloWorld();
    hello.main(args);
    }
    }

    Plus, you have code completion from the Groovy script available to your Java class.

  6. Run the application and you have "Hello from Groovy!" in the Output window.

Join the discussion

Comments ( 9 )
  • guest Wednesday, October 17, 2007

    I use it almost the same way. But my Groovy classes refer to the Java classes. That's why I need to define the Groovy compilation in the "-post-compile" step - works fine.

    It's getting hard when the Groovy classes refer Java classes and the Java classes refer Groovy classes. Groovy 1.1 provides joint compilation, but I've found no way to integrate it in the NetBeans build process.

    BTW: When will a syntax highlighting plugin for Groovy be available? I always see the nice screenshots in your blog. I've tried to create it by myself using your instructions, but it doesn't work in NB 6 Beta 1 :-(

    Bye,

    Stefan


  • movk Wednesday, October 17, 2007

    I'm also looking forward for support of Groovy in NB. Joint compilation seems very promising.

    Possibility of freely mixing java and groovy sources and using joint compilation is something that leaves Ruby far behind. Just imagine, you can write groovy code, that use profiler to find code that needs to be optimized (=rewritten in Java). This may speed up development quite significantly.

    Thanks for all your "Groovy" work that you do in NB.


  • Ray Tayek Friday, October 19, 2007

    hi, trying nb out for a new study group on groovy/grails. seems klunky in that nb does not seem to know anything about groovy classes (no formatting, no code completion, no keyboard to run a groovy class with a main method etc.)

    does your thing help with any of these?

    thanks


  • Geertjan Friday, October 19, 2007

    Nope. But, if you go to the Plugin Manager and search for Groovy, you will find a Groovy Editor that will give you some basic syntax coloring. But, aside from that, NetBeans IDE has no specific support for Groovy at all. Despite that, as shown in this blog entry and in the one from the day before, the relationship between Groovy and Java (as well as NetBeans IDE's integration with Ant) means that one can compile, run, and debug Groovy via Java. (However, work is being done to extend that and Grails project support is nearing completion, if you look at recent blog entries on this suvject in this blog.)


  • Stefan Saturday, October 20, 2007

    Do you mean the Plugin Manager of NetBeans 6.0 Beta1? I've searched there, there was no (basic) Groovy plugin. Unfortunately, because event this would be helpfull ...


  • Geertjan Saturday, October 20, 2007

    It's called "Groovy Editor Support". Look in the "Available" tab. If it isn't in the 6.0 Beta 1, then try a more recent build. I haven't tried this plugin myself, so can't promise anything, but it might provide very basic syntax coloring, possibly. Again, no promises at all.


  • Stefan Sunday, October 21, 2007

    Thanks for the hint, it's not available in Beta 1, I need to update. Can't wait for Beta2 ...


  • Stefan Tuesday, October 23, 2007

    Now I have Beta 2 installe, but there's also no Groovy plugin in the "Available" tab of the Plugin manager :-(

    Have you added another repository source (I only have the default three sources)? Or is this Groovy plugin one of the plugins you've allready created on your system?


  • Geertjan Tuesday, October 23, 2007

    I'm sorry, I don't know, Stefan. I know there was a Groovy plugin available for a while, but it was very basic, it might have been removed.


Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.