Java JAR File – Learn to Create your own File in Java

You might have sometimes downloaded some applications in your machine and you would have been asked to extract the jar file to access all the files of the application. This jar file is nothing but the bundle or group of multiple Java class files merged into a single archive file. Java JAR file stands for Java Archive file.

The JAR file can store many kinds of files like images, video, or audio files. The JAR file decreases the downloading time because the JAR file is the compressed version of a normal file therefore the size is reduced.

There should be a Java Runtime Environment in your machine to open a JAR file in Windows operating system. If it is not there, then you can also use decompression software to access the files in the JAR.

In this article, we will learn about JAR files and how to create, view (decompress), extract, run, and update the JAR files in Java.

What is a JAR file in Java?

The basis of the JAR file format is the zip file format. A JAR file contains Java classes and other executable files. We mainly use the JAR files to implementing the libraries. We can organize our Java programs in Jar files. And, with the help of classpath, we can also use the existing Java code via Jar file. If you need to use the classes of JAR files in your application, you have to add the JAR file to your classpath.

To perform basic operations related to the JAR file, we use the Java Archive Tool (JAR tool). This tool is provided by the Java Development Kit (JDK).

The Manifest File in Jar File

A manifest file is a special file that is present in the META-INF directory and named as MANIFEST.MF. The manifest file contains special meta-information to describe how to use the JAR file. For example, configuring a Classpath, setting the entry point, setting version information.

We can specify our entry point by using the e option, and the jar command will add it to the generated manifest file.

Example to run a jar file with an entry point specified:

jar cfe MyJar.jar com.techvidvan.jar.JarExample com/techvidvan/jar/*.class

Creating JAR Files in Java

To create a Jar file in Java, we use the jar command. We need to use the cf option where c indicates that we are creating a file and f specifies the file. The following is the syntax of writing:

JAR command:

jar cf jarfilename inputfiles

Example:

jar cf MyJar.jar MyClas.class

This command creates a jar file MyJar.jar

Creating an Executable Jar File using the Jar Tool

The jar tool provides many switches, some of them are as follows:

  1. -c creates a new jar file.
  2. -v displays the included or extracted resource on the standard output (verbose output).
  3. -m includes manifest information from the given manifest file.
  4. -f specifies the jar filename.
  5. -x extracts files from the jar file.

Viewing JAR Files in Java

We can use the tf command to view the contents of .jar files.

The syntax is:

jar tf jarfilename

Example:

jar tf MyJar.jar

Extracting JAR Files in Java

We can use the jar command using xf which represents the extraction of a jar file.

The syntax is:

jar xf jarfilename

Example:

jar xf MyJar.jar

The following screenshot contains a MyJarFile folder which is created after running the extract command:

Updating the JAR Files in Java

We can use the jar command to update the contents of a jar file using uf where u indicates update and f indicates a file.

The syntax is:

jar uf jarfilename inputfile(s)

Example:

jar uf MyJar.jar

Running a JAR file in Java

To run a jar file we use the java command.

The syntax is:

java -jar jarfilename

Example:

java -jar MyJar.jar

Verbose Output

To get a verbose output we can have more information out of the jar command. For this, we can simply add the v option for verbose.

jar cvfm MyJar.jar inputfile

Summary

The Jar file is used to aggregate or collect many class files, audio files, image files, or directories into a single file. In this article, we learned to create, view, extract, update, and run JAR files in Java. We also discussed the manifest file Java that stores a meta-information about the jar files.

That was all for Java JAR Files. Thank you for reading our article. Do share your feedback through the comment section below.

Happy Learning 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.