On 14/05/2011, at 1:38 PM, phil swenson wrote:
> let me clean it up. You should be able to copy paste this directly to your
> system and try it. For some reason, the class isn't found. I tried this
> with the Base64 example Roger posted and was able to get a Base64 class
> lookup (Class.forName) to work properly. So it seems there is something odd
> about the Xerces class....
>
> buildscript {
> repositories {
> mavenCentral()
> }
> dependencies {
> classpath( group:"xerces", name:'xercesImpl', version:'2.9.1')
>
> }
> }
>
> task hello {
> doLast {
> println 'Hello world!'
> Class testClass =
> Class.forName("org.apache.xerces.jaxp.DocumentBuilderFactoryImpl")
It's generally not a good idea to use Class.forName(String) in Groovy.
Class.forName(String) uses the ClassLoader of the calling class. In the case of
a Groovy script, the caller is Groovy. And so the lookup uses the ClassLoader
of Groovy, not the ClassLoader of the script. In the case of a Gradle build
script, those ClassLoaders are different and the Groovy ClassLoader cannot see
the dependencies of your script. And so, you get a ClassNotFoundException.
You should either pass in a ClassLoader to Class.forName(), or in the case
above, you could just do use 'new DocumentBuilderFactoryImpl()'
--
Adam Murdoch
Gradle Co-founder
http://www.gradle.org
VP of Engineering, Gradleware Inc. - Gradle Training, Support, Consulting
http://www.gradleware.com