What are JAR files?

JAR stands for Java Archive – which means you can store many files (like .class, image, XML, .properties files etc) in one. Following points explain JAR files in detail.

1. A JAR file is used to package and distribute a set of java classes as a library (API’s) to others. This will help giving a complete component in one file to others than individual classes or other files.

2. JAR file is the compressed file format (based on a famous Zip file format) which means – this will help reducing the overall size of all individual files contained in it and downloading the files in a shorter span.

3. You can bundle all required .class files (and other required files like .properties, xml etc) of your Java application in one JAR file and make it a executable ( see, how to create an executable JAR file)  – which means, you can execute your application by simply executing the JAR file. This way the maintenance and deployment becomes very easy.

4. JAR file consist of an optional Manifest file with the name ‘MANIFEST.MF’ and belongs to the optional directtory named ‘META-INF’. The manifest is a special file that can contain information about the files packaged in a JAR file e.g creation date, version number of the product, developer’s or vendor name, entry point of an executable JAR and many other things.

To perform basic tasks with JAR files, you use the Java Archive Tool provided as part of the Java Development Kit.

Common JAR file operations
OperationCommand
To create a JAR filejar cf jar-file input-file(s)
To view the contents of a JAR filejar tf jar-file
To extract the contents of a JAR filejar xf jar-file
To extract specific files from a JAR filejar xf jar-file archived-file(s)
To run an application packaged as a JAR file (requires the Main-class manifest header)java -jar app.jar

where, c -> indicates that you want to create a JAR file.
f -> indicates that you want the output to go to a file rather than to stdout.
Jar-file -> name that you want the resulting JAR file to have.
input-file(s) -> is a space-separated list of one or more files that you want to include in your JAR file.