Java Launcher with automatic JRE installation
From NSIS Wiki
Jump to navigationJump to searchDescription
I mixed follwing scripts. You need the UAC plug-in to compile this.
The script
Note: Be sure to see Talk:Java Launcher with automatic JRE installation for a note about security and how UAC is being used in the code below.
; Java Launcher with automatic JRE installation;----------------------------------------------- Name"Java Launcher"Caption"Java Launcher"; Icon "Java Launcher.ico"OutFile"SampleLauncher.exe" VIAddVersionKey"ProductName""Java Launcher"VIAddVersionKey"Comments""A test comment"VIAddVersionKey"CompanyName""Fake company"VIAddVersionKey"LegalTrademarks""Java Launcher is a trademark of Fake company"VIAddVersionKey"LegalCopyright""Fake company"VIAddVersionKey"FileDescription""Java Launcher"VIAddVersionKey"FileVersion""1.0.0"VIProductVersion"1.0.0.1" !define CLASSPATH "sample.jar"!define CLASS "Sample"!define PRODUCT_NAME "Sample" ; Definitions for Java 6.0!define JRE_VERSION "6.0"!define JRE_URL "http://javadl.sun.com/webapps/download/AutoDL?BundleId=24936&/jre-6u10-windows-i586-p.exe";!define JRE_VERSION "5.0";!define JRE_URL "http://javadl.sun.com/webapps/download/AutoDL?BundleId=22933&/jre-1_5_0_16-windows-i586-p.exe" ; use javaw.exe to avoid dosbox.; use java.exe to keep stdout/stderr!define JAVAEXE "javaw.exe" RequestExecutionLevel user SilentInstallsilentAutoCloseWindowtrueShowInstDetailsnevershow !include"FileFunc.nsh"!insertmacro GetFileVersion !insertmacro GetParameters !include"WordFunc.nsh"!insertmacro VersionCompare !include"UAC.nsh" Section""Call GetJRE Pop$R0 ; change for your purpose (-jar etc.)${GetParameters}$1StrCpy$0'"$R0" -classpath "${CLASSPATH}" ${CLASS} $1' SetOutPath$EXEDIRExec$0SectionEnd ; returns the full path of a valid java.exe; looks in:; 1 - .\jre directory (JRE Installed with application); 2 - JAVA_HOME environment variable; 3 - the registry; 4 - hopes it is in current dir or PATHFunction GetJRE Push$R0Push$R1Push$2 ; 1) Check local JRE CheckLocal: ClearErrorsStrCpy$R0"$EXEDIR\jre\bin\${JAVAEXE}"IfFileExists$R0 JreFound ; 2) Check for JAVA_HOME CheckJavaHome: ClearErrorsReadEnvStr$R0"JAVA_HOME"StrCpy$R0"$R0\bin\${JAVAEXE}"IfErrors CheckRegistry IfFileExists$R00 CheckRegistry Call CheckJREVersion IfErrors CheckRegistry JreFound ; 3) Check for registry CheckRegistry: ClearErrorsReadRegStr$R1HKLM"SOFTWARE\JavaSoft\Java Runtime Environment""CurrentVersion"ReadRegStr$R0HKLM"SOFTWARE\JavaSoft\Java Runtime Environment\$R1""JavaHome"StrCpy$R0"$R0\bin\${JAVAEXE}"IfErrors DownloadJRE IfFileExists$R00 DownloadJRE Call CheckJREVersion IfErrors DownloadJRE JreFound DownloadJRE: Call ElevateToAdmin MessageBoxMB_ICONINFORMATION"${PRODUCT_NAME} uses Java Runtime Environment ${JRE_VERSION}, it will now be downloaded and installed."StrCpy$2"$TEMP\Java Runtime Environment.exe"nsisdl::download /TIMEOUT=30000${JRE_URL}$2Pop$R0;Get the return valueStrCmp$R0"success" +3MessageBoxMB_ICONSTOP"Download failed: $R0"AbortExecWait$2Delete$2 ReadRegStr$R1HKLM"SOFTWARE\JavaSoft\Java Runtime Environment""CurrentVersion"ReadRegStr$R0HKLM"SOFTWARE\JavaSoft\Java Runtime Environment\$R1""JavaHome"StrCpy$R0"$R0\bin\${JAVAEXE}"IfFileExists$R00 GoodLuck Call CheckJREVersion IfErrors GoodLuck JreFound ; 4) wishing you good luck GoodLuck: StrCpy$R0"${JAVAEXE}"; MessageBox MB_ICONSTOP "Cannot find appropriate Java Runtime Environment."; Abort JreFound: Pop$2Pop$R1Exch$R0FunctionEnd ; Pass the "javaw.exe" path by $R0Function CheckJREVersion Push$R1 ; Get the file version of javaw.exe${GetFileVersion}$R0$R1${VersionCompare} ${JRE_VERSION}$R1$R1 ; Check whether $R1 != "1"ClearErrorsStrCmp$R1"1"0 CheckDone SetErrors CheckDone: Pop$R1FunctionEnd ; Attempt to give the UAC plug-in a user process and an admin process.Function ElevateToAdmin UAC_Elevate: !insertmacro UAC_RunElevated StrCmp1223$0 UAC_ElevationAborted ; UAC dialog aborted by user?StrCmp0$00 UAC_Err ; Error?StrCmp1$10 UAC_Success ;Are we the real deal or just the wrapper?Quit UAC_ElevationAborted: # elevation was aborted, run as normal?MessageBoxMB_ICONSTOP"This installer requires admin access, aborting!"Abort UAC_Err: MessageBoxMB_ICONSTOP"Unable to elevate, error $0"Abort UAC_Success: StrCmp1$3 +4;Admin?StrCmp3$10 UAC_ElevationAborted ;Try again?MessageBoxMB_ICONSTOP"This installer requires admin access, try again"goto UAC_Elevate FunctionEnd