MakeUseOf logo

How to Create a JAR File

A laptop computer sitting in front of a window with code on the screen
Unsplash - no attribution
Source - https://unsplash.com/photos/C8NDn4xk9zs
Eric is a Staff Writer at MakeUseOf. He covers articles on Programming and Linux. He has developer experience in writing Java & web applications. Additionally, he loves blockchain & is always up to date with the latest trends.
Sign in to your MakeUseOf account

JAR is an open file standard coded in Java that groups multiple files into one. It is based on the ZIP file format. JAR stands for Java ARchive.

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:

jar cf ArchiveName.jar files

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:

jar cf webapp.jar login.html images/ audio/

You can also use the wildcard symbol * to include all contents of the current directory in the archive:

jar cf webapp.jar *

By default, jar compresses your files. You can use the verbose option v to see the effect of this compression on each file:

jar cfv webapp.jar *

If you wish to remove compression, then you can use the 0 option instead:

jar cfv0 webapp.jar *

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.

AltStyle によって変換されたページ (->オリジナル) /