xerces classpath issue

billbaloney

House pianist
My eyes are blurry; that must mean I have a java classpath issue again.

I have an executable jar that involves, among other things, instantiating an XML reader:

XMLReader parser = org.xml.sax.helpers.XMLReaderFactory.createXMLReader( "org.apache.xerces.parsers.SAXParser" );

The parser, org.apache.xerces.parsers.SAXParser, is in xercesImpl.jar.

If I run this jar from the command line, e.g.,

java -jar thisjar.jar

I get a ClassNotFoundException:

Exception: java.lang.ClassNotFoundException: org.apache.xerces.parsers.SAXParser
java.lang.NullPointerException

Right. So then I include all of the xerces jars on the classpath:

java -cp xercesImpl.jar:xml-apis.jar:xmlParserAPIs.jar thisjar.jar

and I still get the ClassNotFoundException. Any ideas?

I can replicate this on different OSes too, which makes me suspicious that I'm doing something stupid. Which is what always, always happens when I start wrestling with java's labyrinthine world of classpaths.

TIA.
 
The ClassNotFoundException you get in the 2nd example, where you specify all the .jar files in the classpath, is it the same class that is missing? Or a different class (i.e. it can't find the class with the main method to run)?
 
No, the Main-Class in the jar and that class's main() are found fine, and execute. I get the identical error from the first example:

Exception: java.lang.ClassNotFoundException: org.apache.xerces.parsers.SAXParser
java.lang.NullPointerException

Any ideas? There's actually quite a bit of griping around concerning this general problem; it seems that people might run into some versioning issues or something similar with xerces (e.g., JVM version vs. xerces version).

Thanks again,
Matt
 
Wow, okay. Just so everyone knows, when you run a jar from the command line, i.e., "java -jar thisjar.jar", any classpath setting is ignored. It's a "security feature".

To solve this, write the class path as the "Class-Path:" param in the MANIFEST.MF file. For instance, if you need to run a jar like this:

java -jar -cp /dir/to/otherjar.jar thisjar.jar

The MANIFEST.MF should read like this:

Class-Path: /dir/to/otherjar.jar

Multiple paths are divided by spaces, not colons.

If you can't or don't want to alter the MANIFEST.MF file, just call the Main-Class instead of the jar itself.

java -cp /dir/to/otherjar.jar:thisjar.jar com.domain.myclass

OK.
 
Back
Top