Cannot instantiate DocumentBuilderFactory in JDK 5
Few months back I moved to JDK 5. After this move, recently, I tried running some Acceptance tests using an old fitnesse.jar, which was compiled using JDK 1.4. And guess what? I get the following exception :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | javax.xml.parsers.FactoryConfigurationError: Provider com.sun.org.apache.xerces.internal.jaxp. DocumentBuilderFactoryImpl could not be instantiated: java.lang.NullPointerException [java] at javax.xml.parsers.DocumentBuilderFactory.newInstance (DocumentBuilderFactory.java:104) [java] at fitnesse.util.XmlUtil.<clinit> [java] at fitnesse.wiki.WikiPageProperties.loadFromXmlStream [java] at fitnesse.wiki.FileSystemPage.attemptToReadPropertiesFile [java] at fitnesse.wiki.FileSystemPage.loadAttributes [java] at fitnesse.wiki.FileSystemPage.makePageData [java] at fitnesse.wiki.CachingPage.getData [java] at fitnesse.responders.run.FitClientResponder.readyToSend [java] at fitnesse.responders.run.PuppetResponse.readyToSend [java] at fitnesse.FitNesseExpediter.sendResponse [java] at fitnesse.FitNesseExpediter.start [java] at fitnesse.FitNesseServer.serve [java] at fitnesse.FitNesseServer.serve [java] at fitnesse.socketservice.SocketService$ServerRunner.run [java] at java.lang.Thread.run(Thread.java:595) |
If I use JDK 1.4, everything works fine. But there seems to be some backward compatibility issues with JDK 1.5.
After trying different things for a while, I came across a site that stated that “The J2SE 1.4 platform included the ‘Crimson‘ reference implementation for JAXP 1.1. The J2SE 5 platform includes a reference implementation for JAXP 1.3 based on the Apache ‘Xerces‘ library.â€
In JDK 1.4, xerces and xalan are embedded in the JDK. In JDK 1.5 too, but the packages are under com/sun (so com.sun.org.apache….).
So you would think all this should be transparent to developers. But no.
To verify the above statement, I opened up the DocumentBuilderFactory in JDK 1.5 and I found
1 2 3 4 5 6 7 8 9 10 11 12 | public static DocumentBuilderFactory newInstance() { try { return (DocumentBuilderFactory) FactoryFinder.find( /* The default property name according to the JAXP spec */ "javax.xml.parsers.DocumentBuilderFactory", /* The fallback implementation class name */ "com.sun.org.apache.xerces.internal.jaxp. DocumentBuilderFactoryImpl"); } catch (FactoryFinder.ConfigurationError e) { throw new FactoryConfigurationError(e.getException(), e.getMessage()); } |
}
while the JDK 1.4‘s DocumentBuilderFactory class had:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | public static DocumentBuilderFactory newInstance() throws FactoryConfigurationError { try { return (DocumentBuilderFactory) FactoryFinder.find( /* The default property name according to the JAXP spec */ "javax.xml.parsers.DocumentBuilderFactory", /* The fallback implementation class name */ "org.apache.crimson.jaxp. DocumentBuilderFactoryImpl"); } catch (FactoryFinder.ConfigurationError e) { throw new FactoryConfigurationError (e.getException(), e.getMessage()); } } |
After all this research I still don‘t know what is the exact problem. But I have a solution.
Solution: Luckily adding the xerces.jar to the classpath solved the problem.
Would appreciate if someone would let me know the answer.