title: Executing groovy file in Java
body: <p>I am trying to execute a .groovy file in Java however being new to both Java and Groovy I am having some problems. I'm doing this to learn more and would appreciate if someone could tell me what i am doing wrong. </p>
  
  <pre><code>import groovy.lang.GroovyClassLoader;
  import groovy.lang.GroovyObject;
  import groovy.lang.GroovyShell;
  
  import javax.naming.Binding;
  import java.io.File;
  
  public class testClass extends GroovyShell{
  
      public static void main(String[] args){
  
          try{
              ClassLoader parent = testClass.class.getClassLoader();
              GroovyClassLoader loader = new GroovyClassLoader(parent);
              Class groovyClass = loader.parseClass(new File("src/testg.groovy"));
  
              GroovyObject groovyObject = (GroovyObject) groovyClass.newInstance();
              Object[] args1 = {};
              groovyObject.invokeMethod("run",args1);
  
  
          }catch(Exception e) {
          System.out.println("error loading file");
               }
      }
  
  }
  </code></pre>
  
  <p>I am getting the following errors : </p>
  
  <pre><code>Groovyc: Cannot compile Groovy files: no Groovy library is defined for module 'Prep'
  Using javac 1.7.0_09 to compile java sources
  Compilation completed with 1 error and 0 warnings in 9 sec
  1 error
  0 warnings
  Groovyc: Internal groovyc error: code 1
  </code></pre>
  
  <p>Or perhaps someone could give me an example of how to execute e.g. hello world script written in groovy, in java.</p>