title: Compile Groovy class at runtime in Java
body: <p>I am successfully able to compile Groovy in Java at runtime and store it in a database and pull it out. I can't compile a Groovy class if it has inner classes or an inner enum. Has anyone successfully compiled Groovy code like this and included inner classes/enums and able to pull the script out by classname?</p>
  
  <p>For example, I want to load the "Test" script shown below that contains inner classes and run the script at run time.</p>
  
  <p>Compiler code:  </p>
  
  <pre><code>public byte[] compileGroovyScript(final String className, final String script) {
      byte[] compiledScriptBytes = null;
      CompilationUnit compileUnit = new CompilationUnit();
      compileUnit.addSource(className, script);
      compileUnit.compile(Phases.CLASS_GENERATION);
  
      for (Object compileClass : compileUnit.getClasses()) {
          GroovyClass groovyClass = (GroovyClass) compileClass;
          compiledScriptBytes = groovyClass.getBytes();
      }
  
      return compiledScriptBytes;
  }
  </code></pre>
  
  <p>Code to pull script out:  </p>
  
  <pre><code>public Class getGroovyScript(final String className, final byte[] script) {
      Class clazz = null;
  
      try (GroovyClassLoader classLoader = new GroovyClassLoader(this.getClass().getClassLoader())) {
          clazz = classLoader.defineClass(className, script);
      } catch (IOException e) {
      } catch (Exception e) {
      }
  
      return clazz;
  }
  </code></pre>
  
  <p>Code to run the script:  </p>
  
  <pre><code>Class groovyClass = app.getGroovyScript(className, compiledScript);
  TestScript script = (TestScript) groovyClass.newInstance();
  System.out.println(script.getMessage());
  </code></pre>
  
  <p>Groovy script:  </p>
  
  <pre><code>import com.groovy.groovy.TestScript
  
  class Test implements TestScript {
  
      String getMessage() {
          [1..10].each(){
              println it
          }
          return "Jello"
      }
  }
  </code></pre>