Java Swing GUI

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.
 

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:


 
 

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.

Make sure your manifest file ends with a new line, otherwise your manifest file will not be parsed.

 

 

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:
 

 

 

mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

One thought on “How to Create Executable JAR File in Java

  • Fabio Geronimo

    Obrigado. Este conteúdo está me ajudando muito no meu desenvolvimento na linguagem java.
    😀

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *