Click to See Complete Forum and Search --> : Helloworld program on Java + XML! I am stuck even to compile with DefaultHandler


Least Lee
December 25th, 2002, 01:19 AM
Hi, I am newbies to use Java XML
Can any body help, i have stuck for a long time.
It should be simple for all who have ever use Java + XML.
I am just compilling helloworld program on DefaultHandler
but fail....

Help pls...


I download Xerces-J-bin.2.2.1 from http://xml.apache.org/dist/

C:\xmltest>javac -classpath c:\xmltest\xercesImpl.jar;c:\xmltest\xercesSamples.j
ar;c:\xmltest\xmlParserAPIs.jar CountSax.java

C:\xmltest>java -classpath c:\xmltest\xercesImpl.jar;c:\xmltest\xercesSamples.ja
r;c:\xmltest\xmlParserAPIs.jar CountSax
Exception in thread "main" java.lang.NoClassDefFoundError: CountSax

C:\xmltest>java CountSax
Exception in thread "main" java.lang.NoClassDefFoundError: org/xml/sax/helpers/D
efaultHandler
at java.lang.ClassLoader.defineClass0(Native Method)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.access$100(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClassInternal(Unknown Source)


Below is the code:
import java.io.*;
import org.xml.sax.*;
import org.xml.sax.helpers.*;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParserFactory;
import javax.xml.parsers.SAXParser;


public class CountSax extends DefaultHandler
{
public static void main(String argv[]) throws Exception
{
if (argv.length!=1)
{
System.err.println("u");
System.exit(1);
}

SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser saxParser;
try
{
saxParser = factory.newSAXParser();
saxParser.parse(new File(argv[0]),new CountSax());

}catch(ParserConfigurationException e)
{
System.out.println("he");
}
catch(SAXException e)
{
}
catch(IOException e)
{}
}

static private int eltCount = 0;

public void startDocument()
{

}

public void startElement(String name, String localName, String qName)
{
eltCount++;
}

public void endDocument()
{
System.out.println("Total number of elements"+eltCount);
}
}

dlorde
December 27th, 2002, 12:54 PM
Put the current directory (where the CountSax.class file is) on the classpath:

C:\xmltest>java -classpath .;c:\xmltest\xercesImpl.jar;c:\xmltest\xercesSamples.jar;c:\xmltest\xmlParserAPIs.jar CountSax

Note the '.;' on the classpath. If you provide a classpath, the runtime uses nothing else.

Least Lee
December 28th, 2002, 04:19 AM
What a bug!!

I have stuck for a long time....
(How come such a trick?)

Thank you very much for your help.
Otherwise, I may not be able to figure it out forever.....