JAVA – How To Bundle A Java App As An Windows Executable EXE (.jar to .exe)

This question comes up time and time again and there are two programs that I frequently use to build my compiled Java apps into a Windows Executable files. This, of course, limits the environments in which your app can run to Windows only devices, so if you plan to release a ‘Windows only’ version then this could be the solution for you.

It’s worth noting that it’s also possible to bundle a jar app into a Mac OSX specific .app file, which could be something to consider if you intend to release your app to Mac OS. The .app file extension provides Mac users with a familiar file name structure which can add a subtle level of authenticity to your app. I will cover building a jar file into a Mac OS .app file in a future post.

 

So back to Windows…

 

Option 1: JSmooth (Click here to download from SourceForge)

Smooth provides a basic and easy to use interface to build your jar file into an exe. JSmoth frequently uses relative file paths, so I tend to launch within the distribution file of my compiled Java app.

The app opens on the Welcome screen, which is pretty standard. You can navigate through the pages using the menu located at the left-hand side of the screen.

Head to the Executable section
  • Executable Binary: Enter the location and file name for the exe file you wish to output. I’ve chosen to save my output file as MYAPP.exe.
  • Executable Icon: Select and icon image file to be used in your exe file. I’ve chosen to use WORLD.png located in the source of my app.
  • Current Directory:Here you can specify the working directory, your safe to leave this blank unless you implicitly wish to set it.

Head to the Application section

  • Main Class: Enter the name of the main class in your Java app, that is the main initial class called at runtime.
  • Application Arguments: You can optionally enter arguments to be passed to your app at runtime. You can enter multiple arguments separated by a comma.
  • Embedded Jar: Tick the option to Use an embedded jar and specify the name of your compiled Java .jar file. This tells JSmooth the location from which to load your .jar file.
  • Classpath: Here you must enter every external library (.jar file) included in your app. My compiled Java app has my libraries contained in a LIB folder in the same directory as my MYAPP.jar file.
    IMPORTANT
    ! The library paths are relative to your jar file AND your exe. In my example, MYAPP.exe MUST launch from a file location containing the LIB folder holding the libraries.

Head to JVM Selection

  • Minimum JVM Version: If your app requires a specific JVM version or higher, specify here.
  • Maximum JVM Version: If you wish to limit your app to run below a certain JVM, specify here.
  • Bundle JRE with your app – OPTIONAL You can optionally include a bundled Java Runtime Environment with your exe. This enables your app to run an a PC that doesn’t already have a Java installation. It also gives you control over which JVM version gets used to run your app. If the PC already has a JVM installed then your exe will use the bundled version instead of the PC version. Leave this blank to default to the installed JRE.
    Tick Use a JVM bundled with your applicationSpecify a file location, relative to your exe, which contains a full JVM.In my example, I have a folder named JRE_MASTER which contains a full JVM.
    NOTE: You may bundle a 32-BIT JVM with your exe for operation on 64-BIT OS. This could be useful if your app contains 32-BIT libraries.

Go ahead and hit the COG icon at the top of the screen. All being well, you should now have a .exe file bundled from your compile .jar file.

Smooth error messages are usually detailed enough to help locate and fix potential problems.




Option 2: Launch4j (Click here to download from SourceForge)


Launch4J is set out using a tabbed menu located at the top of the screen, opening on the Basic tab. I will describe some of the basic functions required to generate an exe and once familiar with the process you can explore the advanced functions.

 

Basic Tab

  • Output File: Enter the location and file name for the exe file you wish to output. I’ve chosen to save my output file as MYAPP.exe.
  • Jar: Specify the name of your compiled Java .jar file. This tells Launch4J the location from which to load your .jar file.
  • Icon: Select and icon image file to be used in your exe file. I’ve chosen to use WORLD.ico.
  • Java Download URL: http://java.com/download
JRE Tab
  • Bundle JRE with your app – OPTIONALYou can optionally include a bundled Java Runtime Environment with your exe. This enables your app to run an a PC that doesn’t already have a Java installation. It also gives you control over which JVM version gets used to run your app. If the PC already has a JVM installed then your exe will use the bundled version instead of the PC version. Leave this blank to default to the installed JRE.
  • Bundled JRE Path: Specify a file location, relative to your exe, which contains a full JRE.
  • Min JRE Version: If your app requires a specific JRE version or higher, specify here.
  • Max JRE Version: If you wish to limit your app to run below a certain JRE, specify here
Splash Tab
  • Launch4j provides the ability to specify a splash screen, this feature is not available in JSmooth.
  • Tick Enable splash screen
  • Splash File:Specify as location of your launch image
  • Tick Wait for window
  • Timeout [s]: Enter a timeout period in which the splash screen will disappear
Click the COG icon to generate your exe file.
IMPORTANT!: The library paths are relative to your exe. In my example, MYAPP.exe MUST launch from a file location containing the LIB folder holding the libraries. You are not required to implicitly mention each library whilst configuring your exe, as you are with JSmooth.

You should now have a runnable exe file bundled from your compile .jar file. Take some time to explore the advanced functions within the configuration menus.

Once you’re happy with your configuration you may save the settings in XML, i.e. MYAPP.XML.

You can call Launch4j from command line, specifying the XML file as follows:

”C:\Program Files (x86)\Launch4j\launch4j.exe” “C:\TMP\MYAPP.xml”

This process will auto-generate the exe file using the settings saved earlier to XML.






Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s