Packaging Java apps for the Windows/Linux desktop

Question :

Packaging Java apps for the Windows/Linux desktop,

Answer :

I am writing an application in Java for the desktop using the Eclipse SWT library for GUI rendering. I think SWT helps Java get over the biggest hurdle for acceptance on the desktop: namely providing a Java application with a consistent, responsive interface that looks like that belonging to any other app on your desktop. However, I feel that packaging an application is still an issue.

OS X natively provides an easy mechanism for wrapping Java apps in native application bundles, but producing an app for Windows/Linux that doesn’t require the user to run an ugly batch file or click on a .jar is still a hassle. Possibly that’s not such an issue on Linux, where the user is likely to be a little more tech-savvy, but on Windows I’d like to have a regular .exe for him/her to run.

Has anyone had any experience with any of the .exe generation tools for Java that are out there? I’ve tried JSmooth but had various issues with it. Is there a better solution before I crack out Visual Studio and roll my own?

Read More  Using Actionscript 3 to connect to a database

Edit: I should perhaps mention that I am unable to spend a lot of money on a commercial solution.


To follow up on pauxu’s answer, I’m using launch4j and NSIS on a project of mine and thought it would be helpful to show just how I’m using them. Here’s what I’m doing for Windows. BTW, I’m creating .app and .dmg for Mac, but haven’t figured out what to do for Linux yet.

Project Copies of launch4j and NSIS

In my project I have a “vendor” directory and underneath it I have a directory for “launch4j” and “nsis”. Within each is a copy of the install for each application. I find it easier to have a copy local to the project rather than forcing others to install both products and set up some kind of environment variable to point to each.

Script Files

I also have a “scripts” directory in my project that holds various configuration/script files for my project. First there is the launch4j.xml file:

    true    gui    rpgam.jar    rpgam.exe            .    normal        false    false                        1.5.0            preferJre              ..imagessplash.bmp      true      60      true        

And then there’s the NSIS script rpgam-setup.nsis. It can take a VERSION argument to help name the file.

; The name of the installer  Name "RPG Audio Mixer"    !ifndef VERSION      !define VERSION A.B.C  !endif    ; The file to write  outfile "..distinstallerswindowsrpgam-${VERSION}.exe"    ; The default installation directory  InstallDir "$PROGRAMFILESRPG Audio Mixer"    ; Registry key to check for directory (so if you install again, it will   ; overwrite the old one automatically)  InstallDirRegKey HKLM "SoftwareRPG_Audio_Mixer" "Install_Dir"    # create a default section.  section "RPG Audio Mixer"        SectionIn RO        ; Set output path to the installation directory.      SetOutPath $INSTDIR      File /r "..distlayoutwindows"        ; Write the installation path into the registry      WriteRegStr HKLM SOFTWARERPG_Audio_Mixer "Install_Dir" "$INSTDIR"        ; Write the uninstall keys for Windows      WriteRegStr HKLM "SoftwareMicrosoftWindowsCurrentVersionUninstallRPGAudioMixer" "DisplayName" "RPG Audio Mixer"      WriteRegStr HKLM "SoftwareMicrosoftWindowsCurrentVersionUninstallRPGAudioMixer" "UninstallString" '"$INSTDIRuninstall.exe"'      WriteRegDWORD HKLM "SoftwareMicrosoftWindowsCurrentVersionUninstallRPGAudioMixer" "NoModify" 1      WriteRegDWORD HKLM "SoftwareMicrosoftWindowsCurrentVersionUninstallRPGAudioMixer" "NoRepair" 1      WriteUninstaller "uninstall.exe"        ; read the value from the registry into the $0 register      ;readRegStr $0 HKLM "SOFTWAREJavaSoftJava Runtime Environment" CurrentVersion        ; print the results in a popup message box      ;messageBox MB_OK "version: $0"    sectionEnd    Section "Start Menu Shortcuts"    CreateDirectory "$SMPROGRAMSRPG Audio Mixer"    CreateShortCut "$SMPROGRAMSRPG Audio MixerUninstall.lnk" "$INSTDIRuninstall.exe" "" "$INSTDIRuninstall.exe" 0    CreateShortCut "$SMPROGRAMSRPG AUdio MixerRPG Audio Mixer.lnk" "$INSTDIRrpgam.exe" "" "$INSTDIRrpgam.exe" 0  SectionEnd    Section "Uninstall"        ; Remove registry keys      DeleteRegKey HKLM "SoftwareMicrosoftWindowsCurrentVersionUninstallRPGAudioMixer"      DeleteRegKey HKLM SOFTWARERPG_Audio_Mixer        ; Remove files and uninstaller      Delete $INSTDIRrpgam.exe      Delete $INSTDIRuninstall.exe        ; Remove shortcuts, if any      Delete "$SMPROGRAMSRPG Audio Mixer*.*"        ; Remove directories used      RMDir "$SMPROGRAMSRPG Audio Mixer"      RMDir "$INSTDIR"    SectionEnd  

Ant Integration

I have some targets in my Ant buildfile (build.xml) to handle the above. First I tel Ant to import launch4j’s Ant tasks:


I then have a simple target for creating the wrapper executable:


And another target for making the installer:


The top portion of that just copies the necessary files for the installer to a temporary location and the second half executes the script that uses all of it to make the installer.

That’s the answer Packaging Java apps for the Windows/Linux desktop, Hope this helps those looking for an answer. Then we suggest to do a search for the next question and find the answer only on our site.

Disclaimer :

The answers provided above are only to be used to guide the learning process. The questions above are open-ended questions, meaning that many answers are not fixed as above. I hope this article can be useful, Thank you