Class Loading Add comment to Wiki View in Wiki Edit Wiki page Printable Version

I'm getting an "unable to resolve class My Class" error when I try to use a class contained in external .groovy file.

If the problem goes away when you apply groovyc to the .groovy file (compiling it), and you're running on windows, the problem is probably spaces in the current directory structure. Move your files to a path without spaces (e.g., c:\source rather than c:\documents and settings\Administrator\My Documents\source).

How do I load jars and classes dynamically at runtime?

Use the groovy script's classLoader to add the jar file at runtime.

this.class.classLoader.rootLoader.addURL(new URL("file:///path to file"))

Then, use Class.forName to load the class.

def cls = Class.forName("com.mysql.jdbc.Driver").newInstance();

 

Search

Results of your search request can come from various sources: the Groovy website itself, the JIRA issues, the API documentation, as well as a few other interesting Groovy-related blogs.

  By  -  pages  -  views  - last modified