There are various options for compiling Groovy code and then either running it or using the Java objects it creates in Java code. There is an Ant task called groovyc which works pretty similarly to the javac Ant task which takes a bunch of groovy source files and compiles them into Java bytecode. Each groovy class then just becomes a normal Java class you can use inside your Java code if you wish. Indeed the generated Java class is indistinguishable from a normal Java class, other than it implements the GroovyObject interface. The groovyc Ant task is implemented by the Groovyc class. You can see an example of this in action inside Groovy's maven.xml file (just search for 'groovyc') Another option is to use a GroovyClassLoader to load classes dynamically into a Java program and execute them (or use them) directly. The following Java code shows an example... ClassLoader parent = getClass().getClassLoader(); GroovyClassLoader loader = new GroovyClassLoader(parent); Class groovyClass = loader.parse("Foo.groovy"); // lets call some method on an instance GroovyObject groovyObject = (GroovyObject) groovyClass.newInstance(); Object[] args = {}; groovyObject.invokeMethod("run", args); |