Building and Running the Executable JAR
Now that you've configured the manifest file, you're ready to build and run the executable JAR file. The process involves compiling the Java classes, creating the JAR file, and then executing the JAR file.
Building the Executable JAR
To build the executable JAR file, you can use the jar
command-line tool. Here's an example of how to do this on an Ubuntu 22.04 system:
## Compile the Java classes
javac com/labex/example/Main.java
## Create the manifest file
echo "Main-Class: com.labex.example.Main" > MANIFEST.MF
## Create the JAR file with the manifest
jar cvfm my-app.jar MANIFEST.MF com/
In this example, we first compile the Java classes, then create the manifest file with the Main-Class
attribute, and finally use the jar
command to create the executable JAR file, specifying the manifest file and the compiled Java classes.
Running the Executable JAR
Once you've built the executable JAR file, you can run it using the java
command. Here's an example:
java -jar my-app.jar
This command will execute the main class specified in the manifest file, and run your Java application.
You can also double-click the JAR file in a file manager, or use the open
command on macOS, to run the executable JAR file. The operating system will automatically recognize the JAR file as an executable and launch the Java application.
By following these steps, you can easily build and run your own executable JAR files, making it easier to distribute and run your Java applications.