用于 Java 安装的 NSIS 脚本

发布于 2024-10-17 07:47:52 字数 121 浏览 1 评论 0原文

我想使用 NSIS 脚本安装 Java,但我必须知道系统(Windows)中是否安装了 Java。根据注册键我们如何检查Java是否已安装?

有人可以提供一个 NSIS 脚本来根据注册表项检查 Java 安装吗?

I want to install Java using an NSIS script, but i have to know whether Java is installed or not in the system (Windows). based on the register keys how can we check if Java is installed or not?

Can anybody provide an NSIS script to check Java installation based on registery keys?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

老旧海报 2024-10-24 07:47:52

我没有编译它,但我会尝试遵循。我选择基于 如何检测已安装的注册表项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

I didn't compile it, but I would try following. I chose registry key based on How can I detect the installed Sun JRE on Windows?.

;--------------------------------
;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
绿光 2024-10-24 07:47:52

以下代码片段检查是否安装了 Java(jre 或 jdk)。
如果未安装 Java,则会安装它。
该脚本还将 Java 的路径保存在变量 $JavaInstallationPath 中。

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 脚本中的任何位置调用它。

The following fragment of code checks if Java is installed (jre or jdk).
If Java is not installed, it will install it.
The script also saves the path to Java in a variable $JavaInstallationPath.

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  

I think you can also put the above code in a function and call it wherever you like in your NSIS script.

岁月苍老的讽刺 2024-10-24 07:47:52

另一个例子是在线 jre 安装程序和 64/32 位检查

java 最小版本

!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 页面找到它

Another example with online jre installer and 64/32 bit check

java min version

!define JRE_VERSION_6 "1.5"

check function

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

version compare

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

if theire is something missing you will find it at nsis-wiki page

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文