簡體   English   中英

用於java安裝的NSIS腳本

[英]NSIS script for java installation

我想使用NSIS腳本安裝Java,但我必須知道系統中是否安裝了Java(Windows)。 根據注冊碼,我們如何檢查是否安裝了Java?

任何人都可以提供NSIS腳本來檢查基於注冊密鑰的Java安裝嗎?

我沒有編譯它,但我會嘗試跟隨。 我根據如何在Windows上檢測已安裝的Sun JRE選擇了注冊表項

;--------------------------------
;Defines

  !define JavaRegKey 'HKLM "Software\JavaSoft\Java Runtime Environment" ""'

;--------------------------------
;Installer Sections
Section 'Java Runtime' SecJava

  SetOutPath '$TEMP'
  SetOverwrite on
  File 'c:\<yourdir>\javasetup.exe'
  ExecWait '$TEMP\javasetup.exe' $0
  DetailPrint '..Java Runtime Setup exit code = $0'
  Delete '$TEMP\javasetup.exe'

SectionEnd

;--------------------------------
;   Functions
Function .onInit

  ReadRegStr $R0 ${JavaRegKey}
  StrCmp $R0 "" JavaMissing JavaFound

  JavaFound: 
  !insertmacro UnselectSection ${SecJava}
  Goto JavaCheckDone

  JavaMissing:
  !insertmacro SelectSection ${SecJava}

  JavaCheckDone:

FunctionEnd

以下代碼片段檢查是否安裝了Java(jre或jdk)。 如果未安裝Java,則會安裝它。 該腳本還在變量$JavaInstallationPath保存Java的路徑。

Var JavaInstallationPath
Section "Find Java" FINDJAVA    
    DetectTry1:
    StrCpy $1 "SOFTWARE\JavaSoft\Java Runtime Environment"  
    StrCpy $2 0  
    ReadRegStr $2 HKLM "$1" "CurrentVersion"  
    StrCmp $2 "" DetectTry2 JRE 
    JRE:
    ReadRegStr $5 HKLM "$1\$2" "JavaHome"  
    StrCmp $5 "" DetectTry2 GetValue    

    DetectTry2:  
    ReadRegStr $2 HKLM "SOFTWARE\JavaSoft\Java Development Kit" "CurrentVersion"  
    StrCmp $2 "" NoJava JDK 
    JDK:
    ReadRegStr $5 HKLM "SOFTWARE\JavaSoft\Java Development Kit\$2" "JavaHome"  
    StrCmp $5 "" NoJava GetValue

    GetValue:
    StrCpy $JavaInstallationPath $5
    Messagebox MB_OK "Javahome value: $JavaInstallationPath"
    Goto done

    NoJava:  
    Messagebox MB_OK "No Java installation detected. Installing Java."  
    # Install Java
    ExecWait "$INSTDIR\java\jre-6u26-windows-i586.exe"
    Goto DetectTry1

    done:   
    #$JavaInstallationPath should contain the system path to Java
SectionEnd  

我想你也可以將上面的代碼放在一個函數中,並在NSIS腳本中的任何地方調用它。

另一個使用在線jre安裝程序和64/32位檢查的示例

java min版本

!define JRE_VERSION_6 "1.5"

檢查功能

Function DetectJRE
  ;64bit jre und jdk check
  SetRegView 64
  ReadRegStr $2 HKLM "SOFTWARE\JavaSoft\Java Runtime Environment" \
             "CurrentVersion"
  ;StrCmp $2 ${JRE_VERSION_6} done
  ${VersionCompare} $2 ${JRE_VERSION_6} $R0
  StrCmp $R0 "1" done

  ReadRegStr $2 HKLM "SOFTWARE\JavaSoft\Java Development Kit" \
             "CurrentVersion"
  ;StrCmp $2 ${JRE_VERSION_6} done
  ${VersionCompare} $2 ${JRE_VERSION_6} $R0
  StrCmp $R0 "1" done

  ;32bit jre und jdk check
  SetRegView 32
  ReadRegStr $2 HKLM "SOFTWARE\JavaSoft\Java Runtime Environment" \
             "CurrentVersion"
  ;StrCmp $2 ${JRE_VERSION_6} done
  ${VersionCompare} $2 ${JRE_VERSION_6} $R0
  StrCmp $R0 "1" done

  ReadRegStr $2 HKLM "SOFTWARE\JavaSoft\Java Development Kit" \
             "CurrentVersion"
  ;StrCmp $2 ${JRE_VERSION_6} done
  ${VersionCompare} $2 ${JRE_VERSION_6} $R0
  StrCmp $R0 "1" done


  Call GetJRE
  done:
FunctionEnd

版本比較

Function VersionCompare
    !define VersionCompare `!insertmacro VersionCompareCall`

    !macro VersionCompareCall _VER1 _VER2 _RESULT
        Push `${_VER1}`
        Push `${_VER2}`
        Call VersionCompare
        Pop ${_RESULT}
    !macroend

    Exch $1
    Exch
    Exch $0
    Exch
    Push $2
    Push $3
    Push $4
    Push $5
    Push $6
    Push $7

    begin:
    StrCpy $2 -1
    IntOp $2 $2 + 1
    StrCpy $3 $0 1 $2
    StrCmp $3 '' +2
    StrCmp $3 '.' 0 -3
    StrCpy $4 $0 $2
    IntOp $2 $2 + 1
    StrCpy $0 $0 '' $2

    StrCpy $2 -1
    IntOp $2 $2 + 1
    StrCpy $3 $1 1 $2
    StrCmp $3 '' +2
    StrCmp $3 '.' 0 -3
    StrCpy $5 $1 $2
    IntOp $2 $2 + 1
    StrCpy $1 $1 '' $2

    StrCmp $4$5 '' equal

    StrCpy $6 -1
    IntOp $6 $6 + 1
    StrCpy $3 $4 1 $6
    StrCmp $3 '0' -2
    StrCmp $3 '' 0 +2
    StrCpy $4 0

    StrCpy $7 -1
    IntOp $7 $7 + 1
    StrCpy $3 $5 1 $7
    StrCmp $3 '0' -2
    StrCmp $3 '' 0 +2
    StrCpy $5 0

    StrCmp $4 0 0 +2
    StrCmp $5 0 begin newer2
    StrCmp $5 0 newer1
    IntCmp $6 $7 0 newer1 newer2

    StrCpy $4 '1$4'
    StrCpy $5 '1$5'
    IntCmp $4 $5 begin newer2 newer1

    equal:
    StrCpy $0 0
    goto end
    newer1:
    StrCpy $0 1
    goto end
    newer2:
    StrCpy $0 2

    end:
    Pop $7
    Pop $6
    Pop $5
    Pop $4
    Pop $3
    Pop $2
    Pop $1
    Exch $0
FunctionEnd

getJre

Function GetJRE
    MessageBox MB_YESNO "XXXXXX require at least Java 1.5! Do you want to download and install it now?" IDYES doInstall

    Abort
    doInstall:
    StrCpy $2 "$TEMP\Java Runtime Environment.exe"
    nsisdl::download /TIMEOUT=30000 ${JRE_URL} $2
    Pop $R0 ;Get the return value
        StrCmp $R0 "success" +3
        MessageBox MB_OK "Download failed: $R0"
        Quit
    ExecWait $2
    Delete $2  

    Call DetectJRE
FunctionEnd

如果這個東西不見了,你會在nsis-wiki頁面找到它

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM