Java自动更新时更新Windows路径变量?

时间:2009-11-06 22:08:03

标签: java windows path-variables

This question询问如何在Windows中设置路径变量以包含Java bin目录,允许您使用提示符中的javac命令。发布到此问题的解决方案声明您应该硬编码最新Java安装的绝对路径。

在这种情况下恰好是c:\program files\java\jdk1.6.0_16\bin

我相信一旦安装了Java更新17,18及更高版本,你的javac cmd仍将使用这个旧版本,对吗?

我错过了什么吗?有没有办法将其设置为自动使用最新安装的java更新?

3 个答案:

答案 0 :(得分:5)

  

我相信一旦安装了Java更新17,18及更高版本,你的javac cmd仍将使用这个旧版本,对吗?

正确。当java更新时,get的更新是JRE(Java运行时环境)

自动将您的java插件(在浏览器中使用)和您的java命令集设置为最新版本。 (打开终端并键入java -version)

包含javac的SDK(Java SDK)已更改。你必须手动完成。

一种选择是做mhaller所说的。

我进一步使用Windows中的环境变量。

MYPC / RigthClick /属性/ Advaced / EnvironmentVariables /

从那里我将JAVA_HOME添加到所需的JDK路径(c:\ jsdk_x_x_x)并将PATH变量设置为:

 whatever;_it_had;_before;%JAVA_HOME%\bin
  

有没有办法将其设置为自动使用最新安装的java更新?

对于JRE,它会自动设置,对于SDK,您只需修改系统变量并更改JAVA_HOME的值

答案 1 :(得分:2)

使用Java安装程序设置的变量:

set path="%path%;%JAVA_HOME%\bin"

答案 2 :(得分:0)

以下Powershell脚本非常适用于此:

Remove-PathFolders -Folders "*\Java\jdk*" -EnvironmentVariableTarget $([System.EnvironmentVariableTarget]::Machine)
$jdkDp = (Get-ChildItem -Path "C:\Program Files (x86)\Java\jdk*" | Sort-Object name | Select-Object -Last 1).FullName
Add-PathFolders -Folders $($jdkDp + "\bin\") -EnvironmentVariableTarget $([System.EnvironmentVariableTarget]::Machine)
Get-PathFolders -EnvironmentVariableTarget $([System.EnvironmentVariableTarget]::Machine)

使用受here启发的以下自定义函数(您可以在同一UpdateJavaPath.ps1脚本中包含所有内容):

<#
.SYNOPSIS
Gets the list of folders specified in the Path environment variable.

.PARAMETER EnvironmentVariableTarget
Specifies the "scope" to use when querying the Path environment variable
("Process", "Machine", or "User"). Defaults to "Process" if the parameter is
not specified.

.EXAMPLE
.\Get-PathFolders.ps1
C:\Windows\system32\WindowsPowerShell\v1.0\
C:\Windows\system32
C:\Windows
C:\Windows\System32\Wbem
...

Description
-----------
The output from this example lists each folder in the Path environment variable
for the current process.

.EXAMPLE
.\Get-PathFolders.ps1 User
C:\NotBackedUp\Public\Toolbox

Description
-----------
The output from this example assumes one folder
("C:\NotBackedUp\Public\Toolbox") has previously been added to the user's Path
environment variable.
#>
Function Get-PathFolders() {
    param(
        [string] $EnvironmentVariableTarget = "Process")

    Set-StrictMode -Version Latest
    $ErrorActionPreference = "Stop"

    [string[]] $pathFolders = [Environment]::GetEnvironmentVariable(
        "Path",
        $EnvironmentVariableTarget) -Split ";"

    If ($pathFolders -ne $null)
    {
        Write-Output $pathFolders
    }
}

<#
.SYNOPSIS
Adds one or more folders to the Path environment variable.

.PARAMETER Folders
Specifies the folders to add to the Path environment variable..

.PARAMETER EnvironmentVariableTarget
Specifies the "scope" to use for the Path environment variable ("Process",
"Machine", or "User"). Defaults to "Process" if the parameter is not specified.

.EXAMPLE
.\Add-PathFolders.ps1 C:\NotBackedUp\Public\Toolbox
#>
Function Add-PathFolders() {
    param(
        [parameter(Mandatory = $true, ValueFromPipeline = $true)]
        [string[]] $Folders,
        [string] $EnvironmentVariableTarget = "Process")

    begin
    {
        Set-StrictMode -Version Latest
        $ErrorActionPreference = "Stop"

        Write-Verbose "Path environment variable target: $EnvironmentVariableTarget"

        [bool] $isInputFromPipeline =
            ($PSBoundParameters.ContainsKey("Folders") -eq $false)

        [int] $foldersAdded = 0

        [string[]] $pathFolders = [Environment]::GetEnvironmentVariable(
            "Path",
            $EnvironmentVariableTarget) -Split ";"

        [Collections.ArrayList] $folderList = New-Object Collections.ArrayList

        $pathFolders | foreach {
            $folderList.Add($_) | Out-Null
        }
    }

    process
    {
        If ($isInputFromPipeline -eq $true)
        {
            $items = $_
        }
        Else
        {
            $items = $Folders
        }

        $items | foreach {
            [string] $folder = $_

            [bool] $isFolderInList = $false

            $folderList | foreach {
                If ([string]::Compare($_, $folder, $true) -eq 0)
                {
                    Write-Verbose ("The folder ($folder) is already included" `
                        + " in the Path environment variable.")

                    $isFolderInList = $true
                    return
                }
            }

            If ($isFolderInList -eq $false)
            {
                Write-Verbose ("Adding folder ($folder) to Path environment" `
                    + " variable...")

                $folderList.Add($folder) | Out-Null

                $foldersAdded++
            }
        }
    }

    end
    {
        If ($foldersAdded -eq 0)
        {
            Write-Verbose ("No changes to the Path environment variable are" `
                + " necessary.")

            return
        }

        [string] $delimitedFolders = $folderList -Join ";"

        [Environment]::SetEnvironmentVariable(
            "Path",
            $delimitedFolders,
            $EnvironmentVariableTarget)

        Write-Verbose ("Successfully added $foldersAdded folder(s) to Path" `
            + " environment variable.")
    }
}

<#
.SYNOPSIS
Removes one or more folders from the Path environment variable.

.PARAMETER Folders
Specifies the folders to remove from the Path environment variable..

.PARAMETER EnvironmentVariableTarget
Specifies the "scope" to use for the Path environment variable ("Process",
"Machine", or "User"). Defaults to "Process" if the parameter is not specified.

.EXAMPLE
.\Remove-PathFolders.ps1 C:\NotBackedUp\Public\Toolbox
#>
Function Remove-PathFolders(){
    param(
        [parameter(Mandatory = $true, ValueFromPipeline = $true)]
        [string[]] $Folders,
        [string] $EnvironmentVariableTarget = "Process")

    begin
    {
        Set-StrictMode -Version Latest
        $ErrorActionPreference = "Stop"

        Write-Verbose "Path environment variable target: $EnvironmentVariableTarget"

        [bool] $isInputFromPipeline =
            ($PSBoundParameters.ContainsKey("Folders") -eq $false)

        [int] $foldersRemoved = 0

        [string[]] $pathFolders = [Environment]::GetEnvironmentVariable(
            "Path",
            $EnvironmentVariableTarget) -Split ";"

        [Collections.ArrayList] $folderList = New-Object Collections.ArrayList

        $pathFolders | foreach {
            $folderList.Add($_) | Out-Null
        }
    }

    process
    {
        If ($isInputFromPipeline -eq $true)
        {
            $items = $_
        }
        Else
        {
            $items = $Folders
        }

        $items | foreach {
            [string] $folder = $_

            [bool] $isFolderInList = $false

            for ([int] $i = 0; $i -lt $folderList.Count; $i++)
            {
                #If ([string]::Compare($folderList[$i], $folder, $true) -eq 0)
                if (($folderList[$i] -like $folder) -or ($folderList[$i] -eq ""))
                {
                    $isFolderInList = $true

                    Write-Verbose ("Removing folder ($folderList[$i]) from Path" `
                        + " environment variable...")

                    $folderList.RemoveAt($i)
                    $i--

                    $foldersRemoved++
                }
            }

            If ($isFolderInList -eq $false)
            {
                Write-Verbose ("The folder ($folder) is not specified in the Path" `
                    + " list.")

            }
        }
    }

    end
    {
        If ($foldersRemoved -eq 0)
        {
            Write-Verbose ("No changes to the Path environment variable are" `
                + " necessary.")

            return
        }

        [string] $delimitedFolders = $folderList -Join ";"

        [Environment]::SetEnvironmentVariable(
            "Path",
            $delimitedFolders,
            $EnvironmentVariableTarget)

        Write-Verbose ("Successfully removed $foldersRemoved folder(s) from Path" `
            + " environment variable.")
    }
}