Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DOMSource cannot be processed: check that saxon9-dom.jar is on the classpath

Tags:

java

dom

xml

jar

When I run a simple function for updating DOM XML with no jars, it runs correctly. If I put it code into an existing project with many jars, I get this exception

Exception in thread "main" java.lang.RuntimeException: net.sf.saxon.trans.XPathException: DOMSource cannot be processed: check that saxon9-dom.jar is on the classpath

here

  transformer.transform(source, result);

where

  TransformerFactory transformerFactory = TransformerFactory.newInstance();
  Transformer transformer = transformerFactory.newTransformer();
  DOMSource source = new DOMSource(docBuilder.parse(filepath));

How can I solve this without introducing a dependency on this jar? Thanks.

like image 262
Stepan Yakovenko Avatar asked Dec 01 '25 12:12

Stepan Yakovenko


2 Answers

This message is from an older version of Saxon, and you could solve it either by putting saxon9-dom on the classpath as suggested, or by replacing the Saxon jar file with a more recent release.

Generally I would advise against using the JAXP TransformerFactory mechanism for loading your XSLT processor. You need to decide which XSLT processor you want to use, test your application with it, and then make sure that you load this processor in production regardless what might be lying around on the classpath. If you want your application to work with multiple XSLT processors then you can achieve that but it should be done in a controlled way.

like image 196
Michael Kay Avatar answered Dec 04 '25 03:12

Michael Kay


From API doc for TransformerFactory.html#newInstance(), we can see that there are several ways to achieve this.

  1. If you have any other transformer implementation, for example, xalan, move its JAR's to the beginning of classpath.
  2. Add a JVM option: -Djavax.xml.transform.TransformerFactory=<any transformer class you need>.
  3. Specify the property in the properties file "lib/jaxp.properties" in the JRE directory.
  4. Make a fake JAR with only one file META-INF\services\javax.xml.transform.TransformerFactory and this file only contains the default transformer factory of JDK: com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl. And then add it to the beginning of the classpath.
like image 45
longhua Avatar answered Dec 04 '25 03:12

longhua