Java Launcher: Difference between revisions

From NSIS Wiki
Jump to navigationJump to search
(→‎The Script: added comments to the code)
Line 84: Line 84:
</highlight-nsis>
</highlight-nsis>


once you get this working, do check out [[A_slightly_better_Java_Launcher]].
once you get this working, do check out [[A slightly better Java Launcher]] and [[Java Launcher with automatic JRE installation]].


[[Category:Code Examples]]
[[Category:Code Examples]]

Revision as of 03:09, 29 April 2008

Author: Baz (talk, contrib)


Description

Here is a NSIS script which can be used to launch Java programs the easy way.

It will search for the JRE the following way:

 1 - in jre directory (if you have JRE Installed with your application)
 2 - in JAVA_HOME environment variable
 3 - in the registry
 4 - assume java.exe in current dir or PATH

In this example NSIS is not used as an installer but as a windows program.

There's no programming language out there in which it can be done this simple!

Have fun! (can you believe people want you to pay for this!)

The Script

; Java Launcher
;--------------
 
Name "Java Launcher"
Caption "Java Launcher"
Icon "Java Launcher.ico"
OutFile "Java Launcher.exe"
 
SilentInstall silent
AutoCloseWindow true
ShowInstDetails nevershow
 
!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
  Exec $0
SectionEnd
 
Function GetJRE
;
;  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 PATH
 
  Push $R0
  Push $R1
 
  ; use javaw.exe to avoid dosbox.
  ; use java.exe to keep stdout/stderr
  !define JAVAEXE "javaw.exe"
 
  ClearErrors
  StrCpy $R0 "$EXEDIR\jre\bin\${JAVAEXE}"
  IfFileExists $R0 JreFound  ;; 1) found it locally
  StrCpy $R0 ""
 
  ClearErrors
  ReadEnvStr $R0 "JAVA_HOME"
  StrCpy $R0 "$R0\bin\${JAVAEXE}"
  IfErrors 0 JreFound  ;; 2) found it in JAVA_HOME
 
  ClearErrors
  ReadRegStr $R1 HKLM "SOFTWARE\JavaSoft\Java Runtime Environment" "CurrentVersion"
  ReadRegStr $R0 HKLM "SOFTWARE\JavaSoft\Java Runtime Environment\$R1" "JavaHome"
  StrCpy $R0 "$R0\bin\${JAVAEXE}"
 
  IfErrors 0 JreFound  ;; 3) found it in the registry
  StrCpy $R0 "${JAVAEXE}"  ;; 4) wishing you good luck
 
 JreFound:
  Pop $R1
  Exch $R0
FunctionEnd

once you get this working, do check out A slightly better Java Launcher and Java Launcher with automatic JRE installation.