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.
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
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!