Home > Articles > Programming > Java

Like this article? We recommend

Creating JAR Files

To create a JAR file, you use the command format:

jar cf jar-filename.jar file1.txt subdir2/file2.txt subdir3

Let's scrutinize the syntax above. The c option tells the tool we are creating a JAR. The f option informs the tool that we want the output of our creation to be a file (as opposed to the output being sent to standard out). The jar-filename.jar holds the place of the JAR filename we want to create. (By convention, the .jar extension is used. However, it is not required.)

Next, we have a list of the files that we want added to the archive. As is implied in the example, you can include multiple files by specifying their names, separated by spaces. And you can use wildcards (*) in the file specification. Let's suppose that one of the arguments is a directory (subdir2, in our example); the contents of that directory are added to the archive recursively, preserving the path.

When you use the JAR tool for creation, you can use the v option to have the tool report (a verbose) progress of the files being added to the archive.

As stated earlier, JAR files allow for compression. This feature can be turned off by using the 0 option, indicating zero compression is desired. Whether to use compression or not is a thought for you to ponder. In a networked environment, using compression allows java archives to be delivered quicker. However, this comes at a price. The client must decompress the JAR file, before the client can use it; this decompression process can take a while in itself, depending on various factors.

InformIT Promotional Mailings & Special Offers

I would like to receive exclusive offers and hear about products from InformIT and its family of brands. I can unsubscribe at any time.