JAR was initially created with the intention of grouping applet files together to deliver them to a web browser. This ensured faster download speeds for the applets.
However, the JAR format is flexible enough that it can handle other cases.
Why Are JAR Files Useful?
On top of bundling files together, JAR provides lossless file compression to your archived files. This reduces bandwidth when downloading files over the internet, ensuring faster download speeds.
The JAR format is cross-platform, so you don’t have to worry about any compatibility issues between platforms.
Creating a JAR File
To create a JAR file, you can use the jar command-line tool. The basic format is as shown below:
In the above command, the c option instructs jar to create a JAR archive. The f option specifies that the output should go to a file instead of standard output.
ArchiveName indicates the name of the resulting archive file. Notice the .jar file extension at the end.
After the archive name, you need to list the files to include in your archive. To do so, you should use a space-separated list.
You can also include a directory name in this list. If you do so, then the jar tool will recursively include all files in that directory, for example:
You can also use the wildcard symbol * to include all contents of the current directory in the archive:
By default, jar compresses your files. You can use the verbose option v to see the effect of this compression on each file:
If you wish to remove compression, then you can use the 0 option instead:
JAR and ZIP Files
JAR files are good for sharing Java programs over the web, especially if the people you are collaborating with are tech-savvy.
In general, the average computer user is more familiar with using ZIP files, the format that inspired JAR. If you’re bundling non-Java files, for a general audience, ZIP is the better choice.