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.
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:
- Portability: Executable JAR files can be run on any system with a Java Runtime Environment (JRE) installed, making them highly portable.
- 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.
- 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:
- Java Class Files: The compiled Java class files that make up the application.
- Libraries and Dependencies: Any external libraries or dependencies required by the application.
- 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
- Ensure that your Java application is properly structured, with the main class and any necessary dependencies.
- Compile your Java source code to generate the class files.
Creating the Manifest File
- Create a new file named
MANIFEST.MFin theMETA-INFdirectory of your project. - 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
- Open a terminal and navigate to the root directory of your project.
- 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 toolc: Create a new archivev: Verbose outputf: Specify the output file name (app.jar)m: Include the manifest file-C bin/ .: Change directory tobin/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:
- Open a terminal on your Ubuntu 22.04 system.
- Navigate to the directory where the executable JAR file is located.
- 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:
- Ensure that you have a compatible Java Runtime Environment (JRE) installed on your system.
- Check the manifest file to verify that the
Main-Classentry is correct and matches the fully qualified name of your application's main class. - Inspect the JAR file contents using the
jar tf app.jarcommand to ensure that all necessary files are included. - 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.



