我在gradle脚本中加载类时遇到了问题。当我运行这段代码时:
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"
}
}当我运行"gradle hello“时会得到这样的结果: org.apache.xerces.jaxp.DocumentBuilderFactoryImpl”:java.lang.ClassNotFoundException
我怀疑Jaxp实现有问题,但不太了解jaxp是如何工作的。
谢谢你的帮助
发布于 2011-05-16 08:27:51
像这样的东西足够了吗?
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
}发布于 2011-05-31 14:39:28
请尝试使用getClass().getClassLoader()。根本不应该使用Class.forName();它在从Java调用时存在已知问题,并且在从Groovy调用时完全不可靠(通常会得到Groovy库的类加载器,而不是调用者的类加载器)。
https://stackoverflow.com/questions/5999703
复制相似问题