JAR File in Java (Create, View, Extract, Update, Run) – All in One Place
Get Job-ready: Java Course with 45+ Real-time Projects! - Learn Java
Think of the following scenario. You are late for an interview and you do not want your recruiter to have a bad impression. So you hurry and put all your important documents in a file as you gulp your coffee. We have all been there.
Just as you are confident that you are going to ace/crack the interview, you start pondering about the implications if you suddenly lost your file.
The archive you are carrying with you contains your resume, your CV(Yes they are different) and the IDs and certificates. The list goes on.
Maybe you think how Java does the same thing. When you request a page from a Java server, how does it understand which files to load and how does it load all the data on the website at once instead of using multiple requests?
Same reason as to why you put all your files in a single archive instead of running back and forth getting files. Simplicity and ease of distribution.
What is JAR File in Java?
So what exactly is JAR? Simply speaking, this is a format for archiving data. This is similar to you using WinRAR or WinZIP.
One major difference is that JAR files don’t ask you to pay for a premium version.
Simply speaking, a single JAR file can contain compressed class files, interfaces, and even audio files.
Well, let us look at some important points about JAR files in Java:
- This type of archiving is cross-platform in nature.
- This can handle audio and image files as well.
- There is backward compatibility. Without it being backward compatible, developers would have to rewrite existing applets.
- This is written in Java itself.
- This is a standard of bundling up Java applets.
Functions with JAR file in Java
The basic functions of java JAR file are creating, updating, viewing and extracting. Let us look at them one by one.
1. In order to create a JAR file, we have to use the following syntax :
jar cf <jar_package_name> <original_package_name> .
The cf command means create-file. This creates a jar file of the package we are in currently.
2. In order to view the contents of a JAR file in table view, we can use the syntax:
jar tf <jarfilename>.jar. While displaying you may see the manifest file.
So what is it? The manifest file contains the information regarding other files in the archive.
3. When we need to extract a file with the help of a JAR file, we can use the jar xf <jarfilename>.jar.
This action will create a MANIFEST file and a package containing the classes.
4. If we need to run a JAR file, we can use the command java -jar <jarfilename>.jar
5. If we need to update the contents of the JAR file, we can use jar -uf <jarfilename>.jar (input_files)
Manifest files in JAR
The manifest files typically contain metadata about the files inside the JAR. This contains package related information.
However, if a single application is bundled together in a JAR file, then the manifest file should have the definition of the main class.
You can find the manifest in the location META-INF/MANIFEST.MF.
Each archive can contain only a single manifest file in that location. It should have UTF-8 encoding.
The manifest itself can contain information about other files in the archive.
However, the contents of this file depend on the actual purpose of the JAR file.
Package Sealing in Java
In Java, some users can seal packages. This means that the JAR file archives all the classes of the same type.
This is mainly useful for maintaining version consistency in software.
A sample package sealing looks like a name postfixed with a ‘/’ to distinguish it from a file name.
It also includes a seal header.
An example would be
Name:DataFlair/JavaTutorials/
Sealed: True
This is the relative path of the package. Note that the seal heart applies only to DataFlair/JavaTutorials.
You can also use package version naming along with the basic seal headers to provide data about the JAR version.
You can also use manifest files to specify other classes that are essential for the application to run.
How to make an Executable JAR File in Java
What does it mean when we say that the file is an executable JAR file? Well, when you double click on the JAR file, it automatically calls the main method of the program.
When you have JAVA GUI such as frames present inside the main method this will execute that too.
Let us see how we can make a JAR file as executable in Java. The primary thing to do is to create a manifest file.
Let us observe this step by step.
1. Create the Java file which you want to execute.
This is quite self-explanatory. The primary thing that we need to have is a working JAVA file. So we are going to create one which just prints some basic stuff to the screen.
Let us create one now.
package com.dataflair.jarinjava; public class JarClass { public static void main(String[] args) { System.out.println("This is the main method of the class."); System.out.println("If you can see it after executing a JAR file then congratulations ! "); } }
As you can see this is a very simple file. Now we compile it and generate a .class file for it.
2. Generating a manifest file.
A manifest file is essential as it provides important details about the class that we are going to execute. So we have to create one.
As soon as you create a file named “manifest.mf” we can proceed with defining it.
Inside the manifest file, we write “Main-Class: JarClass ”.
An important point to note is that there should be a new line after the class name. (Simply press ‘enter’ after you write the line.)
3. Generating the JAR executable.
After you create all these we will create the JAR executable.
Simply use the syntax
jar -cvxf <manifest_filename>.mf <output_jar_name>.jar <MainClassName>.class
After you create these, you will have a JAR file that is executable right inside the folder. You can go over and execute it.
However, in Linux, you have to modify it to be executable and then execute it.
This is how you can package your java programs and make them executable on any machine. This is what gives JAVA its versatility and power.
Advanced JAR Features
JAR files support several advanced features that can streamline the development process. For example, JAR files can contain resources such as images, audio files, and text files. This allows developers to bundle all the necessary components of an application into a single file, making distribution and deployment easier.
Additionally, JAR files can include dependency information. This specifies the other JAR files that an application requires to function properly. When you run a JAR file, the Java runtime environment (JRE) can automatically locate and load the necessary dependencies.
Summary
In this article, we came to know about JAR files in Java and how they are essential to packaging and developing applications.
We also learned about manifest files and how to make executable files from JAR files. This is an essential concept for all Java developers.
You give me 15 seconds I promise you best tutorials
Please share your happy experience on Google