Hello daniweb,

I am new to java and am trying to wrap my head around parsing XML with Java. I've decided to try DOM with Xerces to start because it seems pretty simple, however I am getting NoClassDefFound errors. I'm sure it's something really simple that I have overlooked due to my inexperience with Java, but I have looked at multiple examples online and have followed their instructions point by point. I'm not doing all that much, so I'm not sure what could be wrong.

I have imported all the libraries that came with Xerces, which I downloaded off the Xerces website (Version 2.9.0). I am using NetBeans 6.7.1 as my IDE.

I have imported the following:

import org.w3c.dom.*;
import org.apache.xerces.parsers.DOMParser;

My code is fairly straightforward. I have my XML file (Test2_level.xml) in the same folder as my java class.

try {
            DOMParser parser = new DOMParser();
            parser.parse("Test2_level.xml");
            Document doc = parser.getDocument();

            NodeList nodes = doc.getElementsByTagName("servlet");
            System.out.println("There are " + nodes.getLength() +
               "  elements.");

        } catch (Exception ex) {
            System.out.println(ex);
        }

Here is the error that I keep getting.

Exception in thread "MyParser" java.lang.NoClassDefFoundError: org/apache/xerces/parsers/DOMParser
	at java.lang.Class.getDeclaredConstructors0(Native Method)
	at java.lang.Class.privateGetDeclaredConstructors(Class.java:2389)
	at java.lang.Class.getConstructor0(Class.java:2699)
	at java.lang.Class.newInstance0(Class.java:326)
	at java.lang.Class.newInstance(Class.java:308)
	
Caused by: java.lang.ClassNotFoundException: org.apache.xerces.parsers.DOMParser
	at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:319)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:254)
	at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:399)
	... 14 more

Recommended Answers

All 8 Replies

You need to add the jar files for those libraries to your project properties. In the Project Propteries window, select Libraries and add those jars in the Compile section.

You need to add the jar files for those libraries to your project properties. In the Project Propteries window, select Libraries and add those jars in the Compile section.

I'm pretty certain I have already done this. Here are the files I have added already.

xml-apis.jar
xercesImpl.jar
resolver.jar
serializer.jar
xercesSamples.jar

Try using the DocumentBuilderFactory instead of explicitly instantiating the parser.

public static void main(String args[]) throws Exception {
   DocumentBuilder builder = DocumentBuilderFactory.newInstance()
         .newDocumentBuilder();
   Document doc = builder.parse(new File("Test.xml"));
   System.out.println(doc.getElementsByTagName("name"));
}

Did you not have a "xerces.jar" file as well? From the online docs it looks like you should, but perhaps "xercesImpl.jar" has replaced that.

Edit: I just pulled down the latest binaries and xerces.jar is not in there, so that's out as a possibility. I still wonder about your project setup, because the error message points to a class path issue.

Try using the DocumentBuilderFactory instead of explicitly instantiating the parser.

public static void main(String args[]) throws Exception {
   DocumentBuilder builder = DocumentBuilderFactory.newInstance()
         .newDocumentBuilder();
   Document doc = builder.parse(new File("Test.xml"));
   System.out.println(doc.getElementsByTagName("name"));
}

Oddly enough this works without any errors. I wonder why this way works but the other way doesn't ?

Because a version of the xerces parser ships with the JDK and is used as the default DocumentBuilderFactory if you haven't registered a different one.

It's not using the libraries that you downloaded, but that may not matter to you at all if you just want a DOM to work with :)

If you have the xercesImpl.jar visible in the Libraries node of your Projects explorer, then you should be able to instantiate that parser directly without the compiler complaining.

Try using the DocumentBuilderFactory instead of explicitly instantiating the parser.

public static void main(String args[]) throws Exception {
   DocumentBuilder builder = DocumentBuilderFactory.newInstance()
         .newDocumentBuilder();
   Document doc = builder.parse(new File("Test.xml"));
   System.out.println(doc.getElementsByTagName("name"));
}

Because a version of the xerces parser ships with the JDK and is used as the default DocumentBuilderFactory if you haven't registered a different one.

It's not using the libraries that you downloaded, but that may not matter to you at all if you just want a DOM to work with :)

The xerces website is rather confusing to a Java newbie like me. It made it seem like I had to download something to get DOM to work. But yeah I suppose this is all I really wanted to play around with anyways. Thanks guys!

Well, if you wanted the latest Xerces libraries, you did need to download them and add them to your project. It just happens that Sun also included a Xerces implementation as the default for the JAXP document builder factory.

It is still worth your time to learn how to set up those libraries within your project, since chances are you'll want to use some third-party jar again in the future.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.