Accueil > Java > Run a Groovy script from a java class

Run a Groovy script from a java class

28/01/2009
  1. import java.io.File;  
  2.   
  3. import groovy.lang.Binding;  
  4. import groovy.util.GroovyScriptEngine;  
  5.   
  6. public class GroovyRunner {  
  7.     /** 
  8.      * Usage: GroovyRunner [groovy file] 
  9.      * @param args 
  10.      * @throws Exception 
  11.      */  
  12.     public static void main(String[] args) throws Exception {  
  13.         if(args.length == 1) {  
  14.             File groovyFile = new File(args[0]);  
  15.             String[] roots = new String[] {groovyFile.getParentFile().getAbsolutePath()};  
  16.             GroovyScriptEngine gse = new GroovyScriptEngine(roots);  
  17.             Binding binding = new Binding();  
  18.             binding.setVariable("args", roots);  
  19.             gse.run(groovyFile.getName(), binding);  
  20.         } else {  
  21.             System.out.println("usage: "+GroovyRunner.class.getName()+" [groovy file]");  
  22.         }  
  23.     }  
  24. }  

Java

Les commentaires sont fermés.