Search code examples
groovyxercesgroovy-grape

Groovy Grape dealing with dependency resolution


I am trying to use org.xhtmlrenderer:core-renderer:R8pre2 in a groovy script, but I get a Linkage error:

Caught: java.lang.LinkageError: loader constraint violation in interface 
itable initialization: when resolving method  
"org.apache.xerces.dom.NodeImpl.getOwnerDocument()Lorg/w3c/dom/Document;" 
the class loader (instance of org/codehaus/groovy/tools/RootLoader) of the 
current class, org/apache/xerces/dom/NodeImpl, and the class loader (instance of 
<bootloader>) for interface org/w3c/dom/Node have different Class objects for  
the type getOwnerDocument used in the signature

I've already googled a lot and found a lot of answer like these:

Dealing with "Xerces hell" in Java/Maven?

XercesImpl in conflict with JavaSE 6's internal xerces implementation. Both are needed... what can be done?

So, one solution could be to use javas endorsed mechanism to resolve the conflict, but I would like to make my script independent from such a "workaround". The script should run out of the box.

Next thing I was giving a try was to exclude the right dependency like this

@Grapes([
    @Grab('org.xhtmlrenderer:core-renderer:R8pre2'),
    @GrabExclude('xml-apis:xml-apis')
])

but didn't succeed...

Any ideas?

PS: here is the script which creates the error:

@Grapes([
    @Grab('org.xhtmlrenderer:core-renderer:R8pre2'),
])
import org.w3c.dom.Document
import javax.xml.parsers.DocumentBuilder
import javax.xml.parsers.DocumentBuilderFactory
def dbf = DocumentBuilderFactory.newInstance()
DocumentBuilder builder = dbf.newDocumentBuilder()
Document doc = builder.parse(new ByteArrayInputStream("<html></html>".getBytes()))

Solution

  • Thanx to @dmahapatro, I checked my configuration and found that I dropped some jars in {usrhome}/.groovy a long time ago. Removed these and now everything work like a charm...