Java Launcher: Difference between revisions

From NSIS Wiki
Jump to navigationJump to search
m (Adding new author and category links.)
(added link to related page)
Line 78: Line 78:
FunctionEnd
FunctionEnd
</highlight-nsis>
</highlight-nsis>
once you get this working, do check out [[A_slightly_better_Java_Launcher]].


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

Revision as of 07:26, 3 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
;
;  Find JRE (Java.exe)
;  1 - in .\jre directory (JRE Installed with application)
;  2 - in JAVA_HOME environment variable
;  3 - in the registry
;  4 - assume java.exe in current dir or PATH
 
  Push $R0
  Push $R1
 
  ClearErrors
  StrCpy $R0 "$EXEDIR\jre\bin\java.exe"
  IfFileExists $R0 JreFound
  StrCpy $R0 ""
 
  ClearErrors
  ReadEnvStr $R0 "JAVA_HOME"
  StrCpy $R0 "$R0\bin\java.exe"
  IfErrors 0 JreFound
 
  ClearErrors
  ReadRegStr $R1 HKLM "SOFTWARE\JavaSoft\Java Runtime Environment" "CurrentVersion"
  ReadRegStr $R0 HKLM "SOFTWARE\JavaSoft\Java Runtime Environment\$R1" "JavaHome"
  StrCpy $R0 "$R0\bin\java.exe"
 
  IfErrors 0 JreFound
  StrCpy $R0 "java.exe"
 
 JreFound:
  Pop $R1
  Exch $R0
FunctionEnd

once you get this working, do check out A_slightly_better_Java_Launcher.