![]() |
VOOZH | about |
A JAR (Java Archive) file is a platform-independent package file format used to bundle Java class files, metadata, and resources (such as images, text files, and configuration files) into a single compressed archive. JAR files simplify distribution, deployment and reuse of Java applications and libraries. In simple terms, a JAR file is a compressed (.zip-like) archive that contains: Compiled .class files, Resource files (images, audio, text, etc.) and Metadata such as the manifest file.
Note: Since JAR files follow the ZIP specification, tools like WinZip, WinRAR, or 7-Zip can be used to view or extract their contents. However, Java provides the jar tool, which is the recommended way to create and manage JAR files.
The jar tool is included with the JDK and is used to create, view, extract, update, and execute JAR (Java ARchive) files. Below are examples demonstrating common operations on JAR files using the jar tool.
To create a JAR file, use the c (create) and f (file name) options.
Syntax:
jar cf jarfilename.jar inputfiles
Example: Assume a package named pack exists in C:\directory:
C:\> jar cf pack.jar pack
This command creates a JAR file named pack.jar containing the pack directory and its contents.
To list the contents of a JAR file, use the t (table) option.
Syntax:
jar tf jarfilename.jar
Example:
C:/> jar tf pack.jar
META-INF/
META-INF/MANIFEST.MF
pack/
pack/Class1.class
pack/Class2.class
META-INF/MANIFEST.MF
Used to store information such as: Main class, Classpath, Versioning details
To extract the contents of a JAR file, use the x (extract) option.
Syntax:
jar xf jarfilename.jar
Example:
C:\> jar xf pack.jar
This extracts the contents into the current directory, recreating the original folder structure.
To update an existing JAR file by adding or replacing files, use the u (update) option.
Syntax:
jar uf jarfilename.jar inputfiles
Example:
C:\> jar uf pack.jar NewClass.class
This command adds or updates NewClass.class in pack.jar.
A JAR file can be executed if it contains a Main-Class entry in its manifest file.
Syntax:
java -jar jarfilename.jar
Example:
C:\> java -jar pack.jar
The manifest must include:
Main-Class: fully.qualified.ClassName
Option | Description |
|---|---|
c | Create a new JAR file |
t | List contents of a JAR |
x | Extract JAR contents |
u | Update an existing JAR |
f | Specify JAR file name |
v | Verbose output |