tl  tr
  Home | Tutorials | Articles | Videos | Products | Tools | Search
Interviews | Open Source | Tag Cloud | Follow Us | Bookmark | Contact   
 Articles > Java > Basics > How to create Java Executable JAR

How to create Java Executable JAR 

The following example shows creating an executable JAR file in Java. First create a simple standalone java application, for instance a simple Swing UI application.

File Name  :  
/JavaExeTest/ExeJARApplication.java 
   
package com.btc;

import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JButton;
import javax.swing.JFrame;

public class ExeJARApplication {

  public static void main (String [] args) {
    
    JFrame frame = new JFrame("My Application");
    frame.getContentPane().add(new JButton("Click Me"));
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
    frame.setSize(200200);
    frame.setLocation(24090);
    frame.setVisible(true);
  }
}
   

Compile and run the application for sanity.

javac -d . ExeJARApplication.java
java com.btc.ExeJARApplication
Create a manifest file manifest.txt with Main-Class attribute. Ensure to enter a new line at the end of manifest file. Otherwise some systems may throw failed to load Main-Class error.
Main-Class: com.btc.ExeJARApplication

Create an executable JAR using jar command from the console (Ensure to include JDK_HOME\bin in Path environment variable).
jar cvfm [jar-file] [manifest-file] [dirs/files]
jar cvfm test.jar manifest.txt com
Run the executable JAR from the console or from "Open with" windows dialog.
java -jar test.jar
It gives the following output,




 
  


  
bl  br