Friday, September 18, 2009

Windows installer for a java swing app

Early this year I contracted a very small project where the desktop java swing app needed to be installed and run on MS-OS(s), since I used a cross-platform java executable wrapper: launch4j to generate an .exe I could then automate an installer with features like: un-installer, detect core dependencies such as JRE and install it not present as well as Adobe reader, registry settings, readme file, logo splash, etc, etc, and more. Simple cmd compiler invokation as: makensis.exe /V4 InstallScriptSample.nsi Resolution: NSIS did the job, scriptable installer with rich set of options. Below is an example configuration I used, take it as a guideline and modify accordingly for your needs. Cheers!
;----------------------------------------------------
; File: InstallScriptSample.nsi
; Installer using -> http://nsis.sourceforge.net
;----------------------------------------------------
; Note replace "MyApp" with desired app name
;----------------------------------------------------

SetCompressor /SOLID lzma

!include "MUI.nsh"
!insertmacro MUI_PAGE_WELCOME
#!insertmacro MUI_PAGE_DIRECTORY
#!insertmacro MUI_PAGE_COMPONENTS
!insertmacro MUI_PAGE_INSTFILES
!insertmacro MUI_UNPAGE_CONFIRM
!insertmacro MUI_UNPAGE_INSTFILES
!define MUI_FINISHPAGE_NOAUTOCLOSE
!define MUI_FINISHPAGE_RUN
!define MUI_FINISHPAGE_RUN_CHECKED
!define MUI_FINISHPAGE_RUN_TEXT "Start MyApp"
!define MUI_FINISHPAGE_RUN_FUNCTION "LaunchLink"
!define MUI_FINISHPAGE_SHOWREADME_CHECKED
!define MUI_FINISHPAGE_SHOWREADME $INSTDIR\README.txt
!insertmacro MUI_PAGE_FINISH

;----------------------------------
!insertmacro MUI_LANGUAGE "English"
;----------------------------------

#Page directory
#Page components
#Page instfiles

;--------------------------------
; Main Install settings
;--------------------------------

Name "MyApp"
Caption "MyApp Installer | Please wait!"
Icon "icon.ico"
#InstallDir "$PROGRAMFILES\MyApp"
InstallDir "c:\MyApp"
OutFile "Setup.exe"
ShowInstDetails show
ShowUninstDetails show
AutoCloseWindow true
RequestExecutionLevel admin
BrandingText "MyApp v1.0"
AllowRootDirInstall false
InstallColors 00FF00 000000
InstallDirRegKey HKCU "Software\MyApp" ""
InstallDirRegKey HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\MyApp" "UninstallString"

#---------------- installer section start ----------------
section

Call Includes
Call CheckJRE
Call InstallJre
Call InstallAdobe

Success:
Call CreateShortCutsAndUninstaller
Goto Done

Done:

# messageBox MB_OK "Installation was successful @ $INSTDIR $\r$\n \
# Please use desktop shortcut [MyApp] to start-up application."

sectionEnd

#---------------- installer section end --------------------


#---------------- uninstaller section start ----------------

section "uninstall"
WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\MyApp" "DisplayName" ""
WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\MyApp" "UninstallString" ""
DeleteRegKey /ifempty HKCU "Software\MyApp"
delete "$DESKTOP\MyApp.lnk"
delete "$STARTMENU\MyApp.lnk"
delete "$SMPROGRAMS\MyApp.lnk"
rmdir /r $INSTDIR

sectionEnd

#---------------- uninstaller section end ------------------

Function LaunchLink
ExecShell "" "$INSTDIR\MyApp.exe"
FunctionEnd


;--------------------------------
Function Includes

WriteRegStr HKCU "Software\MyApp" "" $INSTDIR

SetOverwrite on

SetOutPath $INSTDIR

File run.cmd
File MyApp.exe
File README.txt
File QA.txt

SetOutPath $INSTDIR\config
File .\config\config.xml
File .\config\hbm.cfg.xml
File .\config\log4j.xml

SetOutPath $INSTDIR\db
File .\db\HSQLDB.app.log
File .\db\HSQLDB.backup
File .\db\HSQLDB.data
File .\db\HSQLDB.properties
File .\db\HSQLDB.script

SetOutPath $INSTDIR\img
File /r ".\img\*.*"

SetOutPath $INSTDIR\lib
File /r ".\lib\*.*"

SetOutPath $INSTDIR\rpt
File /r ".\rpt\*.*"

SetOutPath $INSTDIR\inc
File .\inc\jre-6u11-windows-i586-p.exe
File .\inc\AdbeRdr90_en_US.exe

SetOverwrite off

FunctionEnd

;--------------------------------

Function InstallJre

ExecWait '$INSTDIR\inc\jre-6u11-windows-i586-p.exe /quiet' $0

FunctionEnd

;--------------------------------

Function InstallAdobe

ExecWait '$INSTDIR\inc\AdbeRdr90_en_US.exe /sAll /rs' $0

FunctionEnd

;--------------------------------

Function CreateShortCutsAndUninstaller

#    push $0
#	FileOpen $0 $INSTDIR\MyApp.cmd w
#	FileWrite $0 "cd $INSTDIR\bin$\r$\n"
#	FileWrite $0 "run.cmd$\r$\n"	
#	FileClose $0
#    pop $0

CreateShortCut "$DESKTOP\MyApp.lnk" "$INSTDIR\MyApp.exe" \

"" "$INSTDIR\img\icon.ico" 0 SW_SHOWNORMAL CONTROL|SHIFT|F5 "MyApp"



CreateShortCut "$SMPROGRAMS\MyApp.lnk" "$INSTDIR\MyApp.exe" \

"" "$INSTDIR\img\icon.ico" 0 SW_SHOWNORMAL CONTROL|SHIFT|F5 "MyApp"



CreateShortCut "$STARTMENU\MyApp.lnk" "$INSTDIR\MyApp.exe" \

"" "$INSTDIR\img\icon.ico" 0 SW_SHOWNORMAL CONTROL|SHIFT|F5 "MyApp"



WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\MyApp" "DisplayName" "MyApp (Uninstall)"

WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\MyApp" "UninstallString" "$INSTDIR\Uninstall.exe"



writeUninstaller "$INSTDIR\Uninstall.exe"



FunctionEnd



;--------------------------------

Function CheckJRE

push $0

push $1


ClearErrors

ReadRegStr $0 HKLM "SOFTWARE\JavaSoft\Java Runtime Environment" "CurrentVersion"

ReadRegStr $1 HKLM "SOFTWARE\JavaSoft\Java Runtime Environment\$0" "JavaHome"

StrCpy $1 "$1\bin\java.exe"

pop $0

exch $1

FunctionEnd

;--------------------------------
; eof
;--------------------------------

0 comments: