Home >Java >javaTutorial >How Do I Create an Executable JAR File for My Java Program?

How Do I Create an Executable JAR File for My Java Program?

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2025-01-02 21:02:44701browse

How Do I Create an Executable JAR File for My Java Program?

Generating Executable JAR Files for Java Programs

When developing Java applications, it's often necessary to distribute them as executable JAR (Java Archive) files. This article provides a comprehensive guide on how to create executable JAR files for a Java program.

Step-by-Step Instructions

1. Compile Java Classes:

Compile the source code of your Java program using the following command:

javac *.java

2. Create a Manifest File:

Create a file named MANIFEST.MF with the following contents:

Manifest-Version: 1.0
Main-Class: <main class name>

Replace

with the fully qualified name of the class that contains the main() method.

3. Combine Files:

Place the compiled class files and the manifest file in the same directory.

4. Create the JAR File:

Use the jar command to create the executable JAR file:

jar cfm <jar-file-name>.jar MANIFEST.MF *.class

Example Code:

Consider the following Java program:

public class JarExample {

    public static void main(String[] args) {
        // Your logic here
    }
}

Manifest File:

Manifest-Version: 1.0
Main-Class: JarExample

Creating the JAR File:

javac *.java
jar cfm jarexample.jar MANIFEST.MF *.class

This command will create an executable JAR file named jarexample.jar. To run the program, execute the following command:

java -jar jarexample.jar

The above is the detailed content of How Do I Create an Executable JAR File for My Java Program?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn