0

I am having trouble getting a class to load in a gradle script. When I run this code:

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")
        assert testClass: "org.apache.xerces.jaxp.DocumentBuilderFactoryImpl not found"
        println "found"
    }
}

I get this when I run "gradle hello": java.lang.ClassNotFoundException: org.apache.xerces.jaxp.DocumentBuilderFactoryImpl"

I suspect Jaxp implemenation issues, but don't know much about how jaxp works.

Thanks for any help

phil swenson
  • 8,564
  • 20
  • 74
  • 99

2 Answers2

0

Would something like this suffice?

import org.apache.xerces.jaxp.DocumentBuilderFactoryImpl;

buildscript {
    repositories {
       mavenCentral()
    }

    dependencies {
        classpath group:"xerces", name:'xercesImpl', version:'2.9.1'
    }
}

task hello {
        println 'Hello world!'
        DocumentBuilderFactoryImpl obj = new DocumentBuilderFactoryImpl()
        // do something with obj
}
c_maker
  • 19,076
  • 1
  • 24
  • 29
0

Try getClass().getClassLoader() instead. Class.forName() shouldn't be used at all; it has known problems when called from Java, and is totally unreliable when called from Groovy (you'll typically get the Groovy library's class loader instead of the caller's class loader).

Peter Niederwieser
  • 121,412
  • 21
  • 324
  • 259