XML Parser problem
Hi,
I am having trouble using the Xerces XML parser, when I attempt to create a new instance of the DocumentBuilder I get the error:
javax.xml.parsers.FactoryConfigurationError: Provider org.apache.xerces.jaxp.DocumentBuilderFactoryImpl not found
I have been searching for a couple of days now and have tried using:
System.setProperty("javax.xml.parsers.DocumentBuilderFactory","org.apache.xerces.jaxp.DocumentBuilderFactoryImpl");
as recommended in another thread, I have imported all required jar files into Eclipse and if I create an instance of 'org.apache.xerces.jaxp.DocumentBuilderFactoryImpl' explicitly in code it compiles so the libarary is available.
I am from a C++/C# background with only limited knowledge of the Java environment and so could be missing something blindingly obvious but I am at a loss as to what it is so any help would be greatly appreciated.
Thanks.
[984 byte] By [
danc81a] at [2007-11-27 6:41:41]

> as recommended in another thread, I have imported all
> required jar files into Eclipse and if I create an
> instance of
> 'org.apache.xerces.jaxp.DocumentBuilderFactoryImpl'
> explicitly in code it compiles so the libarary is
> available.
No! The existence of class org.apache.xerces.jaxp.DocumentBuilderFactoryImp is not checked when your program is compiled since it only knows the class exists when told so when your System.setProperty() statement is executed.
It is almost certain that your runtime (not compile time) class path does not contain the jar file that contains the class org.apache.xerces.jaxp.DocumentBuilderFactoryImp .
The classpath is set in the .classpath file in the project folder. If the runtime classpath is the issue, how would I go about adding the required classes to the project so that they end up in the .jar file and the runtime looks for them there?
I am still having trouble getting to grips with classpaths, if I set the runtime to point to the correct classpath containing the Xerces jars that this would only solve the issue for my machine? This is an applet which will appear on a web page and so I need the Xerces classes to be included.
Thanks for your help.
Can anyone shed any light on this? I've still not figured out the problem.
I can see that the classes I require are not contained in my JAR file if I unzip the file but I have them in my classpath and I have tried importing them into the project in Eclipse which does not seem to make a difference.
I have also added each .jar file for the Xerces pakage into my class path in Environment variables and removed & reinstalled Java.
I am not sure what your problem is, however, since you are using eclipse, use the debugger tool. Step into everything to see where it is trying to load the library from. Or, just use class loader to make sure you loading the correct library.Good luck mate
I have used the debugger and I can see that it is the call to 'DocumentBuilderFactory.newInstance()' which is causing the exception. If I look inside the JAR file, the 'DocumentBuilderFactoryImpl.class' is not contained in the jar.
Am I right in thinking that if I want to deploy this on a webpage that that library must be in the jar and setting my JRE classpath to a location containing that library (which so far doesn't help) will not help when a user runs the applet?
How would I go about building all the Xerces classes into my jar?
Well, that needs to be there.For a webpage however, just create a war file. The war file will contains a lib directory and you can put any jars you want in that library. No need to set anything in the classpath.
> How would I go about building all the Xerces classes
> into my jar?
First: why do you need to? If you're just looking to parse XML into a DOM, and are using JDK 1.4 or later, there's a parser that comes with the JDK (Xerces, actually).
Second, make sure that you're putting the correct value into the system property. I'm assuming that you're copying it from someplace.
Actually, scratch that. You shouldn't be setting the system property anyway. If you want to explicitly use Xerces, explicitly create an instance of the Xerces DocumentBuilderFactory. I've seen lots of hard-to-debug problems caused by people who got in the habit of setting system properties, who kept doing so in a shared environment (ie, app-server).
OK, second, make sure that you're loading the _correct_ JARs onto your classpath. Xerces used to provide the implementation JARs separately from the API JARs -- a pre-1.4 hack. Use "jar tvf" on the files listed in your .classpath, and verify that the impl classes are in there somewhere.
Here's what I'm doing.....
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
<snip>
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
I have removed the system property as that was just a suggestion I found previously which did not make any difference. The exception occurs as soon as I try to call newInstance(). The exception is:
javax.xml.parsers.FactoryConfigurationError: Provider org.apache.xerces.jaxp.DocumentBuilderFactoryImpl not found
My Java version is 1.6.0_01.
If the Xerces classes are built in, it should be able to find that provider shouldn't it? And should it be looking for org.apache..... as apposed to java.xml.parsers..... ?