title: Groovy way to dynamically instantiate a class from String
body: <p>The answers of <a href="https://stackoverflow.com/questions/576955/groovy-way-to-dynamically-invoke-a-class">this question about the Groovy way to dynamically invoke a static method</a> were very helpful but I'm having trouble with the following case:</p>
  
  <p>I defined a simple Groovy class:</p>
  
  <pre><code>class Item {
    def id = 1
    def data = [ "a", "b" ]
  }
  </code></pre>
  
  <p>I then defined a simple utility class that wants to dynamically load the Item class:</p>
  
  <pre><code>class Util {
    static def main(args) {
       def cls = "Item" as Class
       def instance = cls.newInstance()
       println instance.toString()
    }
  }
  </code></pre>
  
  <p>Util.groovy is in the same folder as Item.groovy</p>
  
  <p>When I try to run Util.groovy I get the following error:</p>
  
  <pre><code>Caught: org.codehaus.groovy.runtime.typehandling.GroovyCastException: 
  Cannot cast object 'Item' with class 'java.lang.String' 
  to class 'java.lang.Class' due to: 
  java.lang.ClassNotFoundException: Item
          at Util.main(Util.groovy:3)
  </code></pre>
  
  <p>The only way that I could make it work was by using groovyc to precompile Item.groovy, but this misses the point of being Groovy :)</p>