Java Launcher: Difference between revisions

From NSIS Wiki
Jump to navigationJump to search
m (Adding new author and category links.)
m (Reverted edits by 187.115.68.226 to last version by 79.255.88.223)
 
(9 intermediate revisions by 5 users not shown)
Line 12: Line 12:
In this example NSIS is not used as an installer but as a windows program.
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!
There's no programming language out there in which it can be done this simple![citation needed.]


Have fun! (can you believe people want you to pay for this!)
Have fun! (can you believe people want you to pay for this!)
Line 46: Line 46:
Function GetJRE
Function GetJRE
;
;
Find JRE (Java.exe)
returns the full path of a valid java.exe
;  1 - in .\jre directory (JRE Installed with application)
;  looks in:
;  2 - in JAVA_HOME environment variable
;  1 - .\jre directory (JRE Installed with application)
;  3 - in the registry
;  2 - JAVA_HOME environment variable
;  4 - assume java.exe in current dir or PATH
;  3 - the registry
;  4 - hopes it is in current dir or PATH


   Push $R0
   Push $R0
   Push $R1
   Push $R1
  ; use javaw.exe to avoid dosbox.
  ; use java.exe to keep stdout/stderr
  !define JAVAEXE "javaw.exe"


   ClearErrors
   ClearErrors
   StrCpy $R0 "$EXEDIR\jre\bin\java.exe"
   StrCpy $R0 "$EXEDIR\jre\bin\${JAVAEXE}"
   IfFileExists $R0 JreFound
   IfFileExists $R0 JreFound ;; 1) found it locally
   StrCpy $R0 ""
   StrCpy $R0 ""


   ClearErrors
   ClearErrors
   ReadEnvStr $R0 "JAVA_HOME"
   ReadEnvStr $R0 "JAVA_HOME"
   StrCpy $R0 "$R0\bin\java.exe"
   StrCpy $R0 "$R0\bin\${JAVAEXE}"
   IfErrors 0 JreFound
   IfErrors 0 JreFound ;; 2) found it in JAVA_HOME


   ClearErrors
   ClearErrors
   ReadRegStr $R1 HKLM "SOFTWARE\JavaSoft\Java Runtime Environment" "CurrentVersion"
   ReadRegStr $R1 HKLM "SOFTWARE\JavaSoft\Java Runtime Environment" "CurrentVersion"
   ReadRegStr $R0 HKLM "SOFTWARE\JavaSoft\Java Runtime Environment\$R1" "JavaHome"
   ReadRegStr $R0 HKLM "SOFTWARE\JavaSoft\Java Runtime Environment\$R1" "JavaHome"
   StrCpy $R0 "$R0\bin\java.exe"
   StrCpy $R0 "$R0\bin\${JAVAEXE}"


   IfErrors 0 JreFound
   IfErrors 0 JreFound ;; 3) found it in the registry
   StrCpy $R0 "java.exe"
   StrCpy $R0 "${JAVAEXE}" ;; 4) wishing you good luck
          
          
  JreFound:
  JreFound:
Line 78: Line 83:
FunctionEnd
FunctionEnd
</highlight-nsis>
</highlight-nsis>
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]]
[[Category:Java]]

Latest revision as of 17:24, 1 June 2012

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![citation needed.]

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.