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
Release: 2025-01-02 21:02:44
Original
670 people have browsed it

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
Copy after login

2. Create a Manifest File:

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

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

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
Copy after login

Example Code:

Consider the following Java program:

public class JarExample {

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

Manifest File:

Manifest-Version: 1.0
Main-Class: JarExample
Copy after login

Creating the JAR File:

javac *.java
jar cfm jarexample.jar MANIFEST.MF *.class
Copy after login

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

java -jar jarexample.jar
Copy after login

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!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template