Run a Groovy script from a java class
28/01/2009
- import java.io.File;
- import groovy.lang.Binding;
- import groovy.util.GroovyScriptEngine;
- public class GroovyRunner {
- /**
- * Usage: GroovyRunner [groovy file]
- * @param args
- * @throws Exception
- */
- public static void main(String[] args) throws Exception {
- if(args.length == 1) {
- File groovyFile = new File(args[0]);
- String[] roots = new String[] {groovyFile.getParentFile().getAbsolutePath()};
- GroovyScriptEngine gse = new GroovyScriptEngine(roots);
- Binding binding = new Binding();
- binding.setVariable("args", roots);
- gse.run(groovyFile.getName(), binding);
- } else {
- System.out.println("usage: "+GroovyRunner.class.getName()+" [groovy file]");
- }
- }
- }