tags:

views:

21

answers:

1

In my project, there are 2 libraries, each of which depend on the XML parsing class java.xml.parsers.DocumentBuilderFactory. Each of these libraries reference the file from different jar (one gets it from a jar called xmlParserAPIs while another gets it from xml-apis-1.0.b2.jar). Unfortunately there are different versions of the class in each of these files so I am seeing runtime errors, depending on the order they are loaded. Both of these xml jars are transitive dependencies of 3rd party libraries. Is there a good way to handle this conflict?

edit: I'm not sure if it makes a difference on how to handle the problem but this only happens in testing because one of the dependencies is in the test scope.

thanks, jeff

+2  A: 

(...) Unfortunately there are different versions of the class in each of these files so I am seeing runtime errors, depending on the order they are loaded.

In theory, xml-apis.jar and xmlParserAPIs.jar (from xerces2-j) are the same JARs but with different names, xmlParserAPIs.jar being deprecated for years (see this message and this one).

If your dependencies relies on different and incompatible versions of xml-apis.jar, I would say that these dependencies are mutually exclusive, in other words incompatible, at least for the versions you're using. The only solution would be to find versions with a converging dependency.

In case they could use compatible versions, use a dependency exclusion for xmlParserAPIs.jar to use xml-api.jar only.

I'm not sure if it makes a difference on how to handle the problem but this only happens in testing because one of the dependencies is in the test scope.

No, this just explains why you don't get the problem at runtime (because the test scoped is not on the classpath then and, obviously, doesn't conflict).

Pascal Thivent
Thanks. It looks like I have conflicts with xml libraries in 2 other libs. If I exclude both the xmlParserAPIs and xml-apis from the jars and just let the jre provide those, everything works well. Is this an unsafe thing to do? And should I be added a system scoped dependency for java.xml.parsers? Thanks.
Jeff Storey
@Jeff 1. Use `mvn dependency:tree` to analyze your dependencies. 2. If it works with the parser of the JRE, then just exclude the dependencies. 3. I tend to avoid `system` scoped dependencies (more troubles than benefits in my opinion).
Pascal Thivent
@Pascal, thanks for your help.
Jeff Storey