View RSS Feed

My Java Tips

Creating JAR files

Rate this Entry
by , 11-28-2011 at 07:02 PM (2009 Views)
This post is all about learning how to create JAR files. After going through this, you will be able to easily create JAR files.

The basic JAR command syntax is as follows:

jar cf myjar input-file(s)

Let me explain the command;

- c option indicates that I want to create a JAR file
- f option indicates that I want the output to go to a file rather than to stdout
- myjar is the name that I want the JAR file to have. It can be any name you wish to have.
(Convention is to give JAR file name with the .JAR extension. This is not mandatory)
- input-file(s) argument is a list of one or more files that we want to include in the JAR file. Its space-separated list.
We may use wildcard * symbol in the argument.
- c and f options can appear in either order (without space)

jar cf myjar input-file(s) - CORRECT
jar fc myjar input-file(s) - CORRECT
jar c f myjar input-file(s) - INCORRECT

jar cf myjar input-file(s)

The above command will also generate a compressed JAR file in the current directory. A manifest file for the JAR archive will also be generated.

An interesting information for you is that metadata in the JAR file (entry names, comments, contents of the manifest are encoded in UTF8).

So far we have seen the JAR command in the simplest form. There are few additional options to the cf options of the basic command which are given below:

v - V is for verbose output on stdout. This option can be used if you are interested in seeing the name of each file as it's added to the JAR file.

0 - Zero is used if you dont want to compress the JAR file.

M - M is used if you do not want the default manifest.

m - m can be used to include manifest information from an existing manifest file (jar cmf existing-manifest jar-file input-file(s))

I hope this was useful.

Submit "Creating JAR files" to Facebook Submit "Creating JAR files" to Digg Submit "Creating JAR files" to Submit "Creating JAR files" to StumbleUpon Submit "Creating JAR files" to Google

Java SE