This is what I use, if there are additional/improved version I'd be highly interested as well...
Function DetectJava
# this function detects Sun Java from registry and calls the JavaFind utility otherwise
# This is the first time we run the JavaFind
StrCpy $5 "No"
StartCheck:
# First get the installed version (if any) in $2
# then get the path in $6
StrCpy $0 "SOFTWARE\JavaSoft\Java Runtime Environment"
# Get JRE installed version
ReadRegStr $2 HKLM $0 "CurrentVersion"
StrCmp $2 "" DetectTry2
# Get JRE path
ReadRegStr $6 HKLM "$0\$2" "JavaHome"
StrCmp $6 "" DetectTry2
#We seem to have a JRE now
Goto GetJRE
DetectTry2:
# we did not get a JRE, but there might be a SDK installed
StrCpy $0 "Software\JavaSoft\Java Development Kit"
# Get JRE installed version
ReadRegStr $2 HKLM $0 "CurrentVersion"
StrCmp $2 "" RunJavaFind
# Get JRE path
ReadRegStr $6 HKLM "$0\$2" "JavaHome"
StrCmp $6 "" RunJavaFind
GetJRE:
StrCpy $3 "$6\bin\java.exe"
StrCpy $4 "$6\bin\javaw.exe"
# Check if files exists and write paths
IfFileExists $3 0 RunJavaFind
WriteINIStr "$INSTDIR\FLaunch.ini" "Freenet Launcher" "JavaExec" $3
IfFileExists $4 0 RunJavaFind
WriteINIStr "$INSTDIR\FLaunch.ini" "Freenet Launcher" "Javaw" $4
# Jump to the end if we did the Java recognition correctly
Goto End
RunJavaFind:
!ifdef embedJava
# Install Java runtime only if not found
DetailPrint "Lauching Sun's Java Runtime Environment installation..."
SetOutPath "$TEMP"
File ${JAVAINSTALLER}
ExecWait "$TEMP\${JAVAINSTALLER}"
Delete "$TEMP\${JAVAINSTALLER}"
Goto StartCheck
!else
# opening download page
MessageBox MB_YESNO "I did not find Sun's Java Runtime Environment which is needed for Freenet.$\r$\nHit 'Yes' to open the download page for Java (
http://java.sun.com),$\r$\n'No' to cancel the installation." IDYES GetJava
Abort
GetJava:
# Open the download page for Sun's Java
ExecShell "open" "http://javasoft.com/"
Sleep 5000
MessageBox MB_OKCANCEL "Press OK to continue the Freenet installation AFTER having installed Java,$\r$\nCANCEL to abort the installation." IDOK StartCheck
Abort
!endif
AbortJava:
MessageBox MB_OK|MB_ICONSTOP "I still can't find any Java interpreter. Did you really installed the JRE?$\r$\nInstallation will now stop."
Abort
End:
# Restore $0, $2, $5 and $6
Pop $6
Pop $5
Pop $2
Pop $0
FunctionEnd