How to run an executable JAR file

JavaJavaBeginner
Practice Now

Introduction

Java developers often need to package their applications into executable JAR files for easy distribution and deployment. This tutorial will guide you through the process of creating and running executable JAR files, helping you master a fundamental Java programming technique.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL java(("`Java`")) -.-> java/FileandIOManagementGroup(["`File and I/O Management`"]) java/FileandIOManagementGroup -.-> java/files("`Files`") java/FileandIOManagementGroup -.-> java/io("`IO`") java/FileandIOManagementGroup -.-> java/create_write_files("`Create/Write Files`") java/FileandIOManagementGroup -.-> java/delete_files("`Delete Files`") java/FileandIOManagementGroup -.-> java/read_files("`Read Files`") subgraph Lab Skills java/files -.-> lab-414131{{"`How to run an executable JAR file`"}} java/io -.-> lab-414131{{"`How to run an executable JAR file`"}} java/create_write_files -.-> lab-414131{{"`How to run an executable JAR file`"}} java/delete_files -.-> lab-414131{{"`How to run an executable JAR file`"}} java/read_files -.-> lab-414131{{"`How to run an executable JAR file`"}} end

Understanding Executable JAR Files

What is an Executable JAR File?

An executable JAR (Java Archive) file is a special type of Java Archive file that can be run directly from the command line without the need to explicitly invoke the Java Virtual Machine (JVM). This is achieved by including a special entry in the JAR file's manifest file that specifies the main class to be executed.

Why Use Executable JAR Files?

Executable JAR files offer several benefits:

  1. Portability: Executable JAR files can be run on any system with a Java Runtime Environment (JRE) installed, making them highly portable.
  2. Ease of Distribution: Packaging an application as an executable JAR file simplifies the distribution process, as users can simply download and run the file without the need for complex installation procedures.
  3. Dependency Management: Executable JAR files can include all the necessary dependencies, such as libraries and resources, within the JAR file, ensuring a self-contained and reliable deployment.

Key Components of an Executable JAR File

An executable JAR file consists of the following key components:

  1. Java Class Files: The compiled Java class files that make up the application.
  2. Libraries and Dependencies: Any external libraries or dependencies required by the application.
  3. Manifest File: A special file that provides metadata about the JAR file, including the main class to be executed.
graph TD A[Java Class Files] --> B[Libraries and Dependencies] B --> C[Manifest File] C --> D[Executable JAR File]

Manifest File Structure

The manifest file, named MANIFEST.MF, is located in the META-INF directory of the JAR file. It contains a set of key-value pairs that describe the JAR file's contents and properties. The most important entry for an executable JAR file is the Main-Class attribute, which specifies the fully qualified name of the main class to be executed.

Manifest-Version: 1.0
Main-Class: com.labex.app.Main

Creating an Executable JAR File

Preparing the Application

  1. Ensure that your Java application is properly structured, with the main class and any necessary dependencies.
  2. Compile your Java source code to generate the class files.

Creating the Manifest File

  1. Create a new file named MANIFEST.MF in the META-INF directory of your project.
  2. Add the following content to the manifest file:
Manifest-Version: 1.0
Main-Class: com.labex.app.Main

Replace com.labex.app.Main with the fully qualified name of your application's main class.

Packaging the JAR File

  1. Open a terminal and navigate to the root directory of your project.
  2. Run the following command to create the executable JAR file:
jar cvfm app.jar MANIFEST.MF -C bin/ .

This command will create a JAR file named app.jar in the current directory, containing the compiled class files and the manifest file.

The command breakdown:

  • jar: The Java Archive tool
  • c: Create a new archive
  • v: Verbose output
  • f: Specify the output file name (app.jar)
  • m: Include the manifest file
  • -C bin/ .: Change directory to bin/ and include all files in the current directory

Verifying the Executable JAR File

You can verify the contents of the JAR file using the following command:

jar tf app.jar

This will list all the files and directories included in the JAR file.

Running an Executable JAR File

Running the Executable JAR File

To run the executable JAR file, follow these steps:

  1. Open a terminal on your Ubuntu 22.04 system.
  2. Navigate to the directory where the executable JAR file is located.
  3. Run the following command to execute the JAR file:
java -jar app.jar

Replace app.jar with the name of your executable JAR file.

Command-Line Arguments

If your application accepts command-line arguments, you can pass them after the java -jar command:

java -jar app.jar arg1 arg2 arg3

The arguments will be passed to your application's main method.

Troubleshooting

If you encounter any issues while running the executable JAR file, you can try the following:

  1. Ensure that you have a compatible Java Runtime Environment (JRE) installed on your system.
  2. Check the manifest file to verify that the Main-Class entry is correct and matches the fully qualified name of your application's main class.
  3. Inspect the JAR file contents using the jar tf app.jar command to ensure that all necessary files are included.
  4. Review your application's logs or console output for any error messages or clues about the issue.

By following these steps, you should be able to successfully run your executable JAR file on your Ubuntu 22.04 system.

Summary

In this Java tutorial, you have learned how to create and run executable JAR files. By understanding the process of packaging your Java applications into self-contained JAR files, you can simplify the deployment and distribution of your Java programs. This skill is essential for Java developers who want to deliver their applications to end-users or other developers seamlessly.

Other Java Tutorials you may like