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:
Syntax:
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, the pack.jar file is created. In order to view a JAR file '.jar' files, we can use the command as:
Syntax:
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.
META-INF/MANIFEST.MF
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:\
META-INF
In this directory, we can see class1.class and class2.class.
pack
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:
Syntax:
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:
Syntax:
C:\>java -jar pack.jar
Related Article
Working with JAR and Manifest files In Java
Similar Reads
Runtime JAR File in Java JAR stands for Java Archive file. It is a platform-independent file format that allows bundling and packaging all files associated with java application, class files, audio, and image files as well. These files are needed when we run an applet program. It bundles JAR files use a Data compression alg
4 min read
File Handling in Java In Java, with the help of File Class, we can work with files. This File Class is inside the java.io package. The File class can be used to create an object of the class and then specifying the name of the file.Why File Handling is Required?File Handling is an integral part of any programming languag
6 min read
Java Features Java is a high-level, object-oriented programming language. This language is very easy to learn and widely used. It is known for its platform independence, reliability, and security. It follows one principle, that is "Write Once, Run Anywhere" principle. It supports various features like portability
7 min read
Working with JAR and Manifest Files In Java A Jar file stands for Java archives, it is a compressed archive that contains the Java classes, libraries, metadata, and other files such as media data, audio, video, and documents. When we want to distribute a version of the software or distribute a single file, and not a directory structure filled
6 min read
Coding Guidelines in Java Java is one of the most popular and widely used programming languages and platforms. A platform is an environment that helps to develop and run programs written in any programming language. Java is fast, reliable, and secure. From desktop to web applications, scientific supercomputers to gaming cons
7 min read
javap tool in Java with Examples javap tool The javap tool is used to get the information of any class or interface. The javap command (also known as the Java Disassembler) disassembles one or more class files. Its output depends on the options used (â-câ or â-verboseâ for byte code and byte code along with innards info, respective
2 min read