Search

Dark theme | Light theme

November 26, 2009

Groovy Goodness: Compiled Groovy Script is a Java Class

A Groovy script has no class declaration or main() method, but if we compile the script class to Java bytecode, it has. This means we can write a Groovy script, compile the source and use the resulting class in Java.

// File: Sample.groovy
package com.mrhaki.blog

println 'Hello from Groovy'

Next we can compile this script and use Java to invoke the class. We must make sure we add the Groovy library to the classpath, but that is all.

$ groovyc Sample.groovy
$ java -cp .:$GROOVY_HOME/embeddable/groovy-all-1.6.5.jar com.mrhaki.blog.Sample
Hello from Groovy