Monday 14 July 2014

How To Make A Java Exe File Or Executable JAR File

In this tutorial, we will show you how to create a executable JAR – When you double click on it, it runs the defined main class in manifest file.

1. AWT Example

Create a simple AWT Java application, just display label and print out some funny characters ~
AwtExample.java
package com.ashish.awt;
 
import java.awt.Frame;
import java.awt.Label;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
 
public class AwtExample {
 
 public static void main(String[] args) {
 
  Frame f = new Frame();
  f.addWindowListener(new WindowAdapter() {
   public void windowClosing(WindowEvent e) {
    System.exit(0);
   }
  });
  f.add(new Label("This JAR file is executable!"));
  f.setSize(500, 500);
  f.setVisible(true);
 }
}

2. Manifest.txt

Create a manifest.txt file.
Manifest.txt
Main-Class: com.ashish.awt.AwtExample
Uses Main-Class as the entry point of this Jar file, when you double click on this Jar file, the “AwtExample.class” main() method will be launched.
Note
Be sure that your manifest file ends with a new line, else your manifest file will not be parsed and failed to generate the manifest.mf. Read this http://docs.oracle.com/javase/1.4.2/docs/tooldocs/windows/jar.html
Read this jar reference guide :
Be sure that any pre-existing manifest file that you use ends with a new line. The last line of a manifest file will not be parsed if it doesn’t end with a new line character.

No comments:

Post a Comment