Create

Use the following command to create a test.jar JAR file with two class files called A.class and B.class:

jar cf test.jar A.class B.class

The option c indicates that you are creating a new JAR file and the option f indicates that you are specifying a JAR file name, which is test.jar.

At the end of the command, you can specify one or more file names or directory names to include in the JAR file.

View

To view the contents of the test.jar file, use the following command:

jar tf test.jar

The option t lists contents of a JAR file. The option f sets the JAR file name, which is test.jar in this case.

The above command will generate the following output:

META-INF/
META-INF/MANIFEST.MF
A.class
B.class

jar tools created two extra things for you: one directory called META-INF and a file named MANIFEST.MF in the META-INF directory.

The following command creates a test.jar file by including everything in the current working directory.

An asterisk denotes everything in the current working directory.

jar cf test.jar *

The following command creates a JAR file with all class files in the book/archives directory and all images from the book/images directory.

book is a subdirectory in the current working directory.

jar cf test.jar book/archives/*.class book/images

Adding manifest

The the option m uses a manifest.txt file while creating test.jar file including all files and sub-directories in the current directory.

jar cfm test.jar manifest.txt *

The order of the options matters. f before m means that you must specify the JAR file name, test.jar, before the manifest file name manifest.txt.

You can rewrite the above command as follows:

jar cmf manifest.txt test.jar *

Related Topic