Google

Saturday, February 14, 2009

Creating and Using Jar files

Use the previous blog's example.

To create a jar file form the classes folder:

jar -cf SayHelloTest.jar SayHelloTest.class com

To use:

java -classpath ./SayHelloTest.jar SayHelloTest

The dot (.) refers to the present folder AFTER uncompressed.
You need to refer to the . folder because that is where the
main class is found. The main class should not be jarred in
a subfolder.

Also the jar name should be the same as the main class name.

If you have only one folde containing all your classes:

SayHello.class
SayHelloTest.class

Then enter the folder, and do:

jar -cf SayHelloTest.jar *.class

The jar file SayHelloTest.jar will be created in the same folder.
You can copy it anywhere and then run it as follows:

jar -classpath ./SayHelloTest.jar SayHelloTest

or

jar -cp ./SayHelloTest.jar SayHelloTest

The . refers to the classpath after uncompression.
The SayHelloTest.jar that comes after the . refers to the jar file that contains
the classpath for the dot (.)