JAR Files in Java

Basically, we tend to have all our files properly arranged in our systems. No one wishes to take the risk of running back and forth various folders to get a particular set of files. Java also provides this facility to archive our data for ease of distribution. This article will let you understand how the compression of files takes place using Java. Let’s start with java jar files.

What is a JAR file in Java?

The Java Archive file or the JAR file is a format to archive data. It consists of compressed class files, interfaces, and audio files. Comparatively, it is similar to WinRAR and WinZIP. We need to pay a premium to access the WinRAR and WinZIP files, whereas, the JAR files are completely free of cost.

Important points about JAR files in Java:

  • The JAR archiving is cross-platform in nature.
  • It handles audio and images files with ease and is written in Java programming language.
  • It is a standard for bundling up Java applets.
  • The presence of backward compatibility makes JAR files preferable. In the absence of backward compatibility, the developers would have to rewrite the existing applets.

Contents of a JAR file in Java:

When we are looking into the details regarding the JAR file, it is vital to know about the content present in it. usually, the Java applications and their libraries contain an enormous number of various files. These files include the compiled Java source code, a manifest file, XML-based configuration data, JSON-based files, sound clips, images, and even security certificates. A JAR file is generally an aggregation of all these resources into a single, compressed file.

Since a JAR file uses a standard compression algorithm, opening a JAR file is as simple as changing its extension from .jar to .zip and extracting the contents using a standard decompression tool.

It is better to have the JAR files contain a manifest file name MANIFEST.MF in a folder named META-INF. The manifest files contain metadata about the JAR. It also contains properties like code version, primary author of the code, and the organization’s name that maintains the code.

Java Archive Apps:

The installation of the Java Development Kit (JDK) includes a JAR utility to provide various helpful functions to work with the JAR files. This is denoted as jar.sh on Unix and jar.exe on Windows.

It includes the access to:

  • Create new JAR files along with manifest files;
  • Extract every content present in the Java file to place it in the file system.
  • Update an existing JAR file by adding files to it, and
  • Update a JAR file’s existing manifest file.

JAR file openers:

Extracting the content from a file is an important operation. To open a JAR file and extract the contents in it, you should use a couple of JAR utility switches. They are ‘x’ to extract the content and ‘f’ to mention the JAR file’s name.

The JAR utility command that allows extracting the contents of the JAR file named firstcode_jar is:

> jar xf: \java\firstcode_jar.jar

Java JAR command:

The JAR utility also lets you create new Jar files. To create a JAR file named firstcode.jar that contains a couple of files named First.class and Final.class, the command is:

> jar cf Firstcode.jar First.class Final.class

Functions with JAR file in Java:

The basic functions of the Java JAR file are creating, updating, viewing, and extracting.

1. File creation:

The syntax to create a JAR file is:

jar cf <jar_package_name> <original_package_name>

The cf command refers to creating a file. It creates a jar file for the current package.

2. Viewing the contents of the JAR file in table format:

The syntax to view the content of a JAR file is:

jar tf <jafilename>.jar

We can view the manifest file, it contains the information regarding the other files in the archive.

3. Extract a file with the help of a JAR file:

jar xf <jarfilename>.jar

will create a MANIFEST file and a package that contains classes.

4. Running a JAR file:

<jarfilename>.jar

This command runs the java -jar file.

5. Updating the contents of the JAR file:

<jarfilename>.jar (input_files)

It updates the contents of the JAR file.

Manifesting the files in JAR:

The manifest files contain metadata regarding the files that are present inside the JAR. It contains the information related to the package.

The manifest file is present in the location META-INF/MANIFEST.MF. A manifest file is present in each archive in that location. The UTF-8 code is also required for this.

The manifest file contains information regarding the other files in the archive. Yet, the contents of the files depend on the actual purpose of the JAR.

Package Sealing in Java:

In Java, the users can seal the packages. That is the JAR file archives all the classes of the same type. This concept helps in maintaining version consistency in the software.

Example for a package sealing:

A sample package sealing looks like a name that is postfixed with a ‘/’ that distinguishes it from the file name. It also includes a sealed header.

One such example is:

Name: FirstCode/JavaTutorials/

Sealed: True

Here, we can see the relative path of the package. Here, the seal heart applies only to FirstCode/ JavaTutorials.

The package version naming can also be used along with the basic seal headers. This provides data regarding the JAR version.

Manifest files can also be used to specify other classes that are required to run the application.

Creating a JAR file:

The Jar tool provides many switches. Here is a list of such tools:

1. -c it creates a new jar file.

2. -v displays the included or extracted resource on the output.

3. -m it includes manifest information from the manifest file that is given.

4. -f it specifies the jar filename.

5. -x extracts the files from the jar file.

A basic requirement to create a JAR file is a JAVA file. Therefore, we will be creating a JAVA file in the first place.

Sample java program to create a JAR file:

public class firstcodejar{
   public static void main(String args[]){
     
System.out.println("Learn Java with FirstCode");
   }
}

Here, we can clearly see the class and java file for firstcodejar.

The JAR command to create a JAR file is:

jar cf jar-file input-file(s)

Now, we can create a jar file for the class:

This code will generate a jar file for all the classes that are present in the current directory. This happens as we use * instead of name.

Here, you can view the Executable JAR file, fisrtcodejar that has been created.

Viewing JAR files in Java:

The tf command is used to view the contents of the .jar file.

The syntax to view the JAR file is:

jar tf jarfilename

Example command to view the jar files:

jar tf firstcodejar.jar

Extracting the JAR files in Java:

The jar command using xf represents the extraction of a jar file.

The syntax to extract the JAR file is:

jar xf jarfilename

Example command to view the jar files:

jar xf firstcodejar.jar

Updating the JAR files in Java:

The JAR command uf is used to update the contents of a jar file. Here, the u indicates update and f indicates a file.

The syntax to update the JAR files is:

jar uf jarfilename inputfile(s)

An example command to update the JAR files is:

jar uf firstcodejar.jar

Running a JAR file in Java:

The syntax to run a JAR file is:

java -jar jarfilename

An example command to run a JAR file is:

java -jar firstcode.jar

Executable JAR files in Java:

The presence of a Main-Class attribute is vital. It is a stand-alone application that provides the name of the code’s first piece for the Java Runtime Environment (JRE) to invoke the application when it is running.

The term “Executable JAR file” indicates that the JAR file automatically calls the main method of the program when it is double-clicked. Even the JAVA GUI that is present inside the main method will be executed.

Given below are the steps to make a JAR file executable in Java.

1. Create the Java file that to execute

public class FirstCodeJar
{
public static void main(String[] args) {
System.out.println("This main method of the class is executing");
}
} 

Once compiled, it generates a .class file for it.

2. Generating a manifest file:

A manifest file is important as it provides details regarding the class that is to be executed. Therefore, we must create a manifest file first. Here, “manifest.mf” is our manifest file name. Inside this file, the “Main-Class: JarClass” is written.

3. Generating the JAR Executable:

Once all these files are created, we will create the JAR executable.

Syntax:

jar -cvxf <manifest_filename>.mf <output_jar_name>.jar <MainClassName>.class

Once it is created, the JAR executable file will be present inside the folder. You can now execute it.

The only change you have to make while implementing this in a Linux environment is that you have to modify the file to be executable and then execute.

Verbose output:

To get a verbose output, we can obtain more information out of the jar command. The option ‘v’ is added for verbose.

jar cyfm firstcode.jar inputfile

Conclusion:

The Jar file aggregates or collects various class files, audio files, image files, or directories into a single file. You can implement the create, view, extract, update and run JAR files in Java.

Leave a Reply

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