Jim Lawless' Blog


Java in a Windows EXE with launch4j

Originally published on: Sun, 16 Aug 2009 13:46:59 +0000

I noted today that a successful independently written / marketed ( MicroISV ) product had been built in Java. The redistributable files were available for both Windows and MacIntosh computers.

I emailed the author, asking what he used to build the installations for each. I'll discuss the Mac software in a later post. For the Windows installation, he pointed me to a product called launch4j. ( See http://launch4j.sourceforge.net/ ).

Launch4j is an all-Java program that will bind a configurable Windows stub executable file to a Java jar file. The result is a jar embedded in a specially configured EXE.

When invoked, the EXE will execute the system's appropriate JVM ( or will issue an error message if a compatible JVM hasn't been found ) and will then execute the embedded jar file in this JVM.

Effectively, launch4j bottles up Java code into a Windows EXE.

Since launch4j is an all-Java program, one can actually build Windows EXE's on various supported non-Windows platforms.

Let's take a trivial sample Java program and turn it into an EXE with launch4j.

HeyGuys.java


// Display a simple message
// This code is in the public domain.

import javax.swing.*;

public class HeyGuys {
   public static void main(String[] args) {
      JOptionPane.showMessageDialog(null,"Hey, you guys!");
      System.exit(0);
   }
}

Enter the following to compile and execute the program:

javac HeyGuys.java java HeyGuys

The message:

should appear on the screen. Click OK.

Let's take the file HeyGuys.class and put it in its own jar file. We first need to create a manifest file so that the jar will contain metadata to indicate which class should be executed via java.exe or javaw.exe.

Enter the following two lines in a file called manifest.mf: Manifest-Version: 1.0 Main-Class: HeyGuys

Then, issue the following command: jar -cfm heyguys.jar manifest.mf HeyGuys.class

Executing the above command should yield the file heyguys.jar.

Now, let's try to execute the jar to see if we've packaged it up correctly. java -jar heyguys.jar

You should see the same message window that was displayed earlier:

Finally, let's use launch4j to package up the jar into an EXE.

Although launch4j has a command-line interface, I first used the GUI.

The first screen I filled in looked similar to this:

I tried to just save the EXE at that point, but I then needed to specify a minimum Java version on this tab:

After changing those values, launch4j wanted me to save those options in an XML file, so I just chose the name l4j.xml arbitrarily.

Launch4j then quickly packaged a small EXE.

If you run it on a version of Windows with Java 1.6.0 or above, you'll see the message window. If you don't have a 1.6.x JVM, you'll be prompted to download one.

Note that launch4j is not an installer. Now that you have an EXE, you might want to build an installer around it with a product like Inno Setup or the NSIS installer system.

Also note that in addition to the bare options that I've chosen, a number of other options are available including the ability to set an icon and the ability to specify the JVM's runtime parameters such has heap size.

The source code and EXE for the above sample program can be found here:

http://www.mailsend-online.com/wp/heyguys.zip

Unless otherwise noted, all code and text entries are Copyright ©2009 by James K. Lawless



Views expressed in this blog are those of the author and do not necessary reflect those of the author's employer. Views expressed in the comments are those of the responding individual.

stumbleupon Save to StumbleUpon
digg Digg it
reddit Save to Reddit
facebook Share on Facebook
twitter Share on Twitter
aolfav More bookmarks


Previous post: Screen Captures with Java and Clojure
Next post:A Command-Line CD Tray Opener


About Jim ...


Click **here**
to try out MailWrench;
a command-line SMTP /
SMTPS (Google Gmail)
mailer for Windows.


Follow me on Twitter

http://twitter.com/lawlessGuy


Recent Posts

A JavaScript REPL for Android Devices

MailSend is Free

My Blog Engine

The October 10th Bug

A Review of Kevin Mitnick's Book Ghost in the Wires

Spellbound by Web Programming

Backlinks to my Blog Posts

Play MP3 Files with Python on Windows


Random Posts

Locking a Windows Session

A Simple Parser for a Small Command Line Interface

Thwarting HTTP Referer Trackbacks

Blog Posts by Category

An Interview with Brad Templeton

An Interview with the Author of the French Silk Assembler

Preserving my Favorite HN Links

Shrouding CSharp and Java Source Code with AWK

BSave and BLoad for the Commodore 64

Site Tracking with Perl


Full List of Posts

http://www.mailsend-online.com/bloglist.htm


Recent Posts from my Other Blog

Remembering Dr. San Guinary

Why Some Web Sites will go Dark on Jan 18th

SNL Superhero Skit

More Ruby Games

My Ruby Game Challenge Entry

Steal this Bookmarklet

Nerd Toys

Learn New Jargon, You Must

Spot the Wiebe

Tech Magazine Glory Days

Book Review : Paull Allen - Idea Man

A 90's Experiment in Online Systems - The U.S. West CommunityLink Service