How to Create Executable JAR File in Java
In this tutorial, we are going to see how to create an executable JAR file in Java. When you double-click on it, it runs the main class defined in the manifest file.
1. Create a simple application
Let’s first create a simple Java Swing application, which displays a button. The following class must be placed in com/stackhowto/swing/SwingApp.
[st_adsense]
package com.stackhowto.swing; import javax.swing.*; public class SwingApp { public static void main(String[] args){ JFrame frame = new JFrame("My first App"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(250,250); JButton btn = new JButton("Click here"); frame.getContentPane().add(btn); frame.setVisible(true); } }
Output:
[st_adsense]
2. Manifest.txt
Create a manifest.txt file that contains the following line:
Main-Class: com.stackhowto.swing.SwingApp
Uses Main-Class as the entry point to this Jar file, when you double click on this Jar file, the main() method of the SwingApp class will be started.
3. Jar file
Create a Jar file by adding the files “SwingApp.class” and “manifest.txt”.
Let’s assume the structure of your project is as follows:
c:\projet\classes\com\stackhowto\swing\SwingApp.class c:\projet\classes\manifest.txt
Move to the “classes” folder and run the following command to create the JAR file “SwingApp.jar
jar -cvfm SwingApp.jar manifest.txt com/stackhowto/swing/*.class
Output:
[st_adsense]
Obrigado. Este conteúdo está me ajudando muito no meu desenvolvimento na linguagem java.
😀