top

Search

Java Tutorial

.

UpGrad

Java Tutorial

Jar file in Java

In the world of Java programming, JAR files play a significant role in packaging and distributing Java applications. They are a convenient way to bundle multiple Java class files, resources, and libraries into a single archive. If you’re wondering what is JAR full form in Java, the acronym stands for Java ARchive. 

In this comprehensive guide, you will learn everything from creating and viewing JAR files to extracting, updating, and running them, as well as how to use JAR files in Java. We will also throw some light on what is JAR file in Java with examples. 

Overview

Let us begin with the fundamental question: What is a JAR file in Java? 

A JAR file is essentially a compressed archive that contains the compiled Java class files, associated metadata, and resources required by a Java application or library. It simplifies the process of distributing Java programs by providing a single file that encapsulates all the necessary components.

How to Create a JAR File in Java

To create a JAR file in Java, you can use the jar command-line tool that comes bundled with the Java Development Kit (JDK). Let's take a look at an example of creating a JAR file:

  • Create a directory named "myproject" and navigate to it.

  • Compile the Java source files: javac com/example/*.java

  • Create a manifest file named "Manifest.txt" with the following content:

Here is the Java command for producing a JAR file:

jar cvf jar-filename file-input

Let's examine the aforementioned command in more depth. The letter c signifies that we are attempting to generate a jar file, v stands for verbose (which provides more information in the logs than the standard logs); it displays certain command details on the console; and f designates to store the command's output in the file designated in the command prompt. 

The command output is saved in a JAR file with the name jar-filename. The path or filename of the file that needs to be compressed and added to the JAR file is the file input.

JAR File in Java Example

jar cvf SampleJarFile.jar SampleJavaClass.class

The SampleJavaClass.class file will be compressed using the code above and stored in SampleJarFile.jar. 

If the command prompt is open in the current directory, we can specify the file name directly; otherwise, we must provide an absolute path. 

In this instance, we are taking into account the command prompt that is open in the current directory.

View a JAR File in Java

Java offers a mechanism to view the contents of a JAR file, much like how a file system displays the contents of a folder.

The JAR file's contents are displayed when the following command is run. 

jar tf jar-filename

Let's examine the command in more detail. The t suggests that the jar file's contents should be displayed in tabular format, and the f says that the command prompt should contain the jar file's path and filename. Additionally, the command's jar-filename demands the filename of the jar file whose contents are to be displayed.

Here’s an example:

jar tf SampleJarFile.jar

The above code will display the SampleJarFile.jar's contents in a table-like format.

Extracting a JAR File

To extract the contents of a JAR file in Java, you can use the jar command or any file archiving tool. Here's an example of extracting a JAR file using the jar command:

jar xf jar-filename

Let's examine the command in greater detail. The letters x indicate that we are attempting to extract the jar file, and the letters f indicate that the jar filename is given in the command prompt itself.

The files we want to extract can be specified explicitly.

jar xf SampleJarFile.jar SampleJavaClass.class

Only the SampleJavaClass.class will be extracted from the SampleJarFile.jar by the aforementioned command.

We could also extract the entire JAR file.

jar xf SampleJarFile.jar

The SampleJarFile.jar's files will all be extracted using the previously mentioned command.

Without specifying the input files using the 'c' option, the jar tool does not permit the generation of a JAR file. However, if a JAR's program files are explicitly removed, the extracted folder of that file will just contain the manifest file that is created by default.

Running a JAR File in Java

To run a Java application packaged in a JAR file, you can use the Java command followed by the JAR file name. 

Here's an example of running a JAR file:

java -cp jar-filename MainClassName

The -cp in the aforementioned command indicates the classpath, which will run the designated class file. The jar tool will not raise a main manifest attribute error without the -cp parameter.

Consider that SampleJavaClass is the main class in our scenario and that the command to run our jar file is

java -cp SampleJarFile.jar SampleJavaClass

The SampleJavaClass.class file will be run using the aforementioned command. Can a JAR be run without using the -cp command?

Executable JAR Files in Java:

An executable JAR file contains a special entry in its manifest file that specifies the class containing the main method. This allows the JAR file to be executed directly. 

To create an executable JAR file, you need to specify the Main-Class attribute in the manifest file. Here's an example of generating a manifest file and creating an executable JAR:

  1. Create the Java file with a main method.

  2. Generate a manifest file named "Manifest.txt" with the following content:

Manifest-Version: 1.0
Main-Class: com.example.Main
  1. Generate the executable JAR file: jar cvfm myproject.jar Manifest.txt com/example/*.class

How to Make an Executable JAR File in Java?

1. Create the Java File

Let's write a straightforward Java swing program.

Since the result of a swing program is displayed on UI, we have written a swing application to test the execution of the JAR file when double-clicking on the file icon.

A Java program example is shown below:

The code must now be compiled using the following command in order to produce the.class file.

The code will be compiled using the aforementioned command, producing the SimpleSwingExample.class file.

2. Generating a Manifest File

Now, let's write the details of the primary class name into a text file. Let's give it the name manifest.txt. This is the text that must be included in the file.

After this step, we'll have a fresh manifest.txt file. The Main-Class property shouldn't be the last line in the file to prevent incorrect processing. At the conclusion, include an empty line.

3. Making the Executable JAR 3

Let's construct the JAR file now that the manifest has been made. To accomplish this, we must mention the name of this text file in the command line while making the jar file. Here is the code to accomplish it.

We can also build an executable JAR file by including the MainClass name in the command when creating the JAR file. This is the command to carry it out.

The entry point of the JAR file is indicated by option e in the aforementioned command.

Run the following command to run the aforementioned jar files.

Alternatively, you can double-click the JAR file icon, which some operating systems like Windows support. The following GUI will be displayed after running the JAR file.

Java Archive Apps

JAR files are not limited to just Java applications. They can also be used to package Java applets, Java libraries, and even non-Java resources. By including all the necessary files and resources in a single JAR file, you can simplify the deployment process.

JAR File Openers

To open and extract the contents of a JAR file on various platforms, several JAR file opener tools are available, such as 7-Zip, WinRAR, and Java's JAR command-line tool. 

These tools allow you to explore the contents of JAR files and extract specific files or directories.

The Java JAR Command

The JAR command is a powerful tool for working with JAR files. It provides various options for creating, updating, extracting, and viewing JAR files. You can use it from the command line or incorporate it into build scripts to automate the packaging process.

Manifest Files in JAR

Manifest files play a crucial role in JAR files, as they contain metadata about the JAR file and its contents. They can specify the main class, define class-path entries for dependent JAR files, and provide other information required for the proper execution of the JAR file.

Using Maven to Create JAR File in Java

If you are using Maven as your build tool, creating a JAR file becomes even simpler. Maven provides a plugin called "maven-jar-plugin" that automates the process of creating JAR files. By configuring the plugin in your project's POM (Project Object Model) file, you can generate JAR files with ease.

Package Sealing in Java

In Java, package sealing allows you to restrict access to classes and resources within a specific package. By sealing a package, you ensure only classes in the same JAR file can access its contents. This feature provides a level of encapsulation and security for your Java applications.

Using Spring Boot to Create JAR File in Java

Spring Boot, a popular Java framework, simplifies the process of creating executable JAR files for Spring-based applications. It automatically generates a manifest file with the necessary entries and includes all the required dependencies in the JAR file. This makes it incredibly easy to package and deploy Spring Boot applications.

JAR file in Java download

To download a JAR file in Java, follow these steps:

  • Identify the source or website where the JAR file is available.

  • Locate the download link for the JAR file.

  • Click on the download link.

  • Choose a download location on your computer.

  • Wait for the download to complete.

  • Verify the downloaded JAR file in the chosen location.

Conclusion

JAR files are a fundamental part of Java development, offering a convenient way to package and distribute Java applications and libraries. They provide a self-contained archive that simplifies the deployment process.  It makes  understanding JAR files in Java crucial, and this guide has covered all necessary aspects you  have to know before you proceed. 

FAQ

1. What steps can I follow to execute a JAR file in the Java environment?

Ans: To execute a JAR file in the diverse Java environment, you'll need to use the command "java -jar <filename.jar>". This simple, yet powerful command will initiate the execution process for the specified JAR file.

2. What's the process for extracting files from a JAR file in Java?

Ans: When it comes to extracting files from a JAR file in Java, you can rely on the command "jar xf <filename.jar>". Utilizing this command will efficiently unpack the contents of the designated JAR file.

3. How do I add a file to an existing JAR file?

Ans: You can add a file to an existing JAR file using the 'jar uf' command, followed by the filename and the file to add. The syntax is "jar uf <filename.jar> <file>". This command updates the JAR file with the added file.

Leave a Reply

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