A slightly better Java Launcher

From NSIS Wiki

Author: Pac (talk, contrib)


Testing Java Launcher example, I noticed it calls the JRE's "java.exe". The problem with "java.exe" is that it will always open a command window (which must stay open for the whole execution of the child Java process - if the user accidentally closes the command window the child process dies too). So, I thought it would be better to use "javaw.exe". It will open your java application directly as a normal Windows application.

A second modification is to use ExecWait instead of Exec to launch the java process. This avoids a Windows 2000 problem (As described by the Eclipse IDE Windows launcher author in a code comment, "This launcher process must continue to process events until the JVM exits or else Windows 2K will hang if the desktop properties (e.g., background) are changed by the user. Windows does a SendMessage() to every top level window process, which blocks the caller until the process responds."). This may or may not apply here, but the exe process is so small it won't hurt.

See also Java Launcher with automatic JRE installation.

The Modified Code

; Java Launcher
;You want to change the next four lines
Name "YourProgramName"
Caption "Java Launcher"
Icon "YourProgram.ico"
OutFile "YourProgram.exe"
SilentInstall silent
AutoCloseWindow true
ShowInstDetails nevershow
;You want to change the next two lines too
!define CLASSPATH ".;lib;lib\myJar"
!define CLASS "org.me.myProgram"
Section ""
  Call GetJRE
  Pop $R0
  ; change for your purpose (-jar etc.)
  StrCpy $0 '"$R0" -classpath "${CLASSPATH}" ${CLASS}'
  SetOutPath $EXEDIR
  ExecWait $0
Function GetJRE
;  Find JRE (javaw.exe)
;  1 - in .\jre directory (JRE Installed with application)
;  2 - in JAVA_HOME environment variable
;  3 - in the registry
;  4 - assume javaw.exe in current dir or PATH
  Push $R0
  Push $R1
  StrCpy $R0 "$EXEDIR\jre\bin\javaw.exe"
  IfFileExists $R0 JreFound
  StrCpy $R0 ""
  ReadEnvStr $R0 "JAVA_HOME"
  StrCpy $R0 "$R0\bin\javaw.exe"
  IfErrors 0 JreFound
  ReadRegStr $R1 HKLM "SOFTWARE\JavaSoft\Java Runtime Environment"
  ReadRegStr $R0 HKLM "SOFTWARE\JavaSoft\Java Runtime Environment\$R1"
  StrCpy $R0 "$R0\bin\javaw.exe"
  IfErrors 0 JreFound
  StrCpy $R0 "javaw.exe"
  Pop $R1
  Exch $R0
Personal tools