【问题标题】:How to automate setting version of Java that appears first in Path environment variable?如何自动设置首先出现在 Path 环境变量中的 Java 版本?
【发布时间】:2022-10-02 20:49:49
【问题描述】:

我正在尝试在 Windows 中自动设置环境。作为其中的一部分,我想确保 Java 11.0.11 的特定安装首先出现在 Path windows 环境变量中。我目前正在使用 NSIS 来创建安装,并且可以做任何我想做的事情,除了确保我的 Java 版本首先出现在 Path 变量中。听起来 NSIS 不允许这样做,因为在典型的用例中,您希望通过强制像这样的版本更改来防止安装破坏其他已安装的产品(我在这里推测)。请参阅此处接受的答案中的 cmets:How to use NSIS EnVar plug in to edit path?

如何创建一个自动化过程(例如脚本),将我的 Java 版本放在 Path 变量中?

- - 编辑 - - - - - - - - - - - - - -

接受的示例完美运行(甚至在底部包含一个示例用例)。 https://github.com/NACHC-CAD/nsis-examples/tree/main/examples/006-envvar-prepend 提供了完整的工作示例。

    标签: windows nsis


    【解决方案1】:

    改编自this answer

    !include LogicLib.nsh
    !include WinCore.nsh
    
    Function RegPrependString
    System::Store S
    Pop $R0 ; append
    Pop $R1 ; separator
    Pop $R2 ; reg value
    Pop $R3 ; reg path
    Pop $R4 ; reg hkey
    System::Call 'ADVAPI32::RegCreateKey(i$R4,tR3,*p.r1)i.r0'
    ${If} $0 = 0
        System::Call 'ADVAPI32::RegQueryValueEx(pr1,tR2,p0,*i.r2,p0,*i0r3)i.r0'
        ${If} $0 <> 0
            StrCpy $2 ${REG_SZ}
            StrCpy $3 0
        ${EndIf}
        StrLen $4 $R0
        StrCpy $8 $4
        StrLen $5 $R1
        IntOp $4 $4 + $5
        System::Call '*(&t$4s,&t$5,&t$3,&t1"")p.r9' $R0
        ${If} $0 = 0
        ${AndIf} $3 <> 0
          System::Call 'KERNEL32::lstrcat(t)(pr9,tR1)'
          IntOp $8 $8 + $5
        ${EndIf}
         IntOp $3 $3 + 1 ; For \0
        !if ${NSIS_CHAR_SIZE} > 1
          IntOp $3 $3 * ${NSIS_CHAR_SIZE}
          IntOp $8 $8 * ${NSIS_CHAR_SIZE}
        !endif
        IntPtrOp $8 $9 + $8
        System::Call 'ADVAPI32::RegQueryValueEx(pr1,tR2,p0,p0,pr8,*ir3)i.r0'
        ${If} $0 = 0
        ${OrIf} $0 = ${ERROR_FILE_NOT_FOUND}
            System::Call 'KERNEL32::lstrlen(t)(pr9)i.r0'
            IntOp $0 $0 + 1
            !if ${NSIS_CHAR_SIZE} > 1
                IntOp $0 $0 * ${NSIS_CHAR_SIZE}
            !endif
            System::Call 'ADVAPI32::RegSetValueEx(pr1,tR2,i0,ir2,pr9,ir0)i.r0'
        ${EndIf}
        System::Free $9
        System::Call 'ADVAPI32::RegCloseKey(pr1)'
    ${EndIf}
    Push $0
    System::Store L
    FunctionEnd
    
    Section
    
    Push ${HKEY_CURRENT_USER}
    Push "Environment"
    Push "Path"
    Push ";"
    Push "c:\whatever"
    Call RegPrependString
    Pop $0
    DetailPrint RegPrependString:Error=$0
    
    SectionEnd 
    

    这将始终放在前面,首先使用EnVar::Check 来查看它是否已经存在。或者如果特别在前面:

    !include LogicLib.nsh
    StrCpy $0 "c:\whatever"
    ReadRegStr $1 HKCU "Environment" "Path"
    StrCpy $1 "$1;"
    StrCpy $0 "$0;"
    StrLen $2 $0
    StrCpy $1 $1 $2
    ${If} $1 == $0
        DetailPrint "Already prepended"
    ${Else}
        DetailPrint "Does not start with $0"
    ${EndIf}
    

    【讨论】:

      猜你喜欢
      • 2015-01-15
      • 2011-11-26
      • 2014-02-01
      • 2011-02-14
      • 2011-03-31
      • 2016-11-29
      • 2012-10-01
      • 2014-11-04
      • 2016-07-28
      相关资源
      最近更新 更多