Jar files in Java
A JAR (Java Archive) is a package file format typically used to aggregate many Java class files and associated metadata and resources (text, images, etc.) into one file to distribute application software or libraries on the Java platform.
In simple words, a JAR file is a file that contains a compressed version of .class files, audio files, image files, or directories. We can imagine a .jar file as a zipped file(.zip) that is created by using WinZip software. Even, WinZip software can be used to extract the contents of a .jar . So you can use them for tasks such as lossless data compression, archiving, decompression, and archive unpacking.
Let us see how to create a .jar file and related commands which help us to work with .jar files
1.1 Create a JAR file
In order to create a .jar file, we can use jar cf command in the following ways as discussed below:
jar cf jarfilename inputfiles
Here, cf represents to create the file. For example , assuming our package pack is available in C:\directory , to convert it into a jar file into the pack.jar , we can give the command as:C:\> jar cf pack.jar pack
1. 2 View a JAR file
Now, pack.jar file is created. In order to view a JAR file ‘.jar’ files, we can use the command as:
jar tf jarfilename
Here, tf represents the table view of file contents. For example, to view the contents of our pack.jar file, we can give the command:
C:/> jar tf pack.jar
Now, the contents of pack.jar are displayed as follows:
META-INF/ META-INF/MANIFEST.MF pack/ pack/class1.class pack/class2.class .. ..
Here class1, class2, etc are the classes in the package pack. The first two entries represent that there is a manifest file created and added to pack.jar. The third entry represents the sub-directory with the name pack and the last two represent the files name in the directory pack.
Note: When we create .jar files, it automatically receives the default manifest file. There can be only one manifest file in an archive, and it always has the pathname.
This manifest file is useful to specify the information about other files which are packaged.
1.3 Extracting a JAR file
In order to extract the files from a .jar file, we can use the commands below listed:
jar xf jarfilename
Here, xf represents extract files from the jar files. For example, to extract the contents of our pack.jar file, we can write:
C:\> jar xf pack.jar
This will create the following directories in C:\
In this directory, we can see class1.class and class2.class.
1.4 Updating a JAR File
The Jar tool provides a ‘u’ option that you can use to update the contents of an existing JAR file by modifying its manifest or by adding files. The basic command for adding files has this format as shown below:
jar uf jar-file input-file(s)
Here ‘uf’ represents the updated jar file. For example, to update the contents of our pack.jar file, we can write:
C:\>jar uf pack.jar
1.5 Running a JAR file
In order to run an application packaged as a JAR file (requires the Main-class manifest header), the following command can be used as listed:
C:\>java -jar pack.jar
Related Article: Working with JAR and Manifest files In Java
This article is contributed by Nishant Sharma. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.