How do I run a jar file?

I am having problem in creating a runnable jar file. Here is what I have:

My classes are:

/tools/utility/java/src/lib/saxValidate.class - Main class
/tools/utility/java/src/lib/XMLFileFilter.class

Manifest.txt:

Main-Class: saxValidate
Classpath: .:/tools/3rd_party/jdom/lib/jdom.jar:/tools/3rd_party/jdom/lib/xerces.jar:

I did this to build the jar file:

1) jar cvf saxValidate.jar *.class
2) jar cmf Manifest.txt saxValidate.jar  *.class

Error I get:

> java -jar saxValidate.jar
Exception in thread "main" java.lang.NoClassDefFoundError: org/jdom/JDOMException

What is the problem with above?

Thanks.





JoegalAsked:
Who is Participating?
 
objectsConnect With a Mentor Commented:
Class-Path: jdom.jar xerces.jar


:)
0
 
CEHJCommented:
YOu can't put a jar in a jar and have its classes loadable See the question here:
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
CEHJCommented:
>> YOu can't put a jar in a jar and have its classes loadable

(Unless you follow one of the strategies mentioned)
0
 
objectsCommented:
> Classpath: .:/tools/3rd_party/jdom/lib/jdom.jar:/tools/3rd_party/jdom/lib/xerces.jar:

Change that to

Classpath: jdom.jar, xerces.jar

And put those jars in the same directory as saxValidate.jar (instead of inside it)

0
 
objectsCommented:
woops, that should be:

Class-Path: jdom.jar, xerces.jar
0
 
objectsCommented:
one moretime:

Class-Path: jdom.jar xerces.jar

I need some coffee :)
0
 
JoegalAuthor Commented:
I tried this now:

1) I put all the jars in same directory as suggested by objects:

tools/utility/java/src/lib/

2) I changed Manifest.txt:

Main-Class: saxValidate
Classpath: jdom.jar xerces.jar

3) I did this to build the jar file:

          a) jar cvf saxValidate.jar *.class
          b) jar cmf Manifest.txt saxValidate.jar  *.class

4) I get the error:

java -jar saxValidate.jar

Exception in thread "main" java.lang.NoClassDefFoundError: org/jdom/JDOMException


Is there any other way I can run the main class in jar file without having to do above (since above is not working)?

Thanks.

0
 
objectsCommented:
> Classpath: jdom.jar xerces.jar

That should be CLass-Path, not Classpath
0
 
JoegalAuthor Commented:
So silly of me:-(

Thanks objects. It works fine. How do I do it by using "java -cp" instead of running the jar?
0
 
CEHJCommented:
Now move  saxValidate.jar to a location where none of its contained jars is in the file system classpath and see what happens
0
 
objectsCommented:
> How do I do it by using "java -cp" instead of running the jar?

java -classpath saxValidate.jar;jdom.jar;xerces.jar saxValidate
0
All Courses

From novice to tech pro — start learning today.