Refresh Windows path variable when Java updates automatically?

This question asks how to set up a path variable in Windows to include the bin bin directory, allowing you to use the javac command from the prompt. The solution posted to this question states that you must hardcode the absolute path of the latest Java installation.

In this case, it is c:\program files\java\jdk1.6.0_16\bin

I believe that when Java versions 17, 18 and higher are installed, your javac cmd will still use this older version, right?

Am I missing something ??? Is there a way to install this to automatically use the latest installed java update?

+3
java windows path-variables
Nov 06 '09 at 22:08
source share
3 answers

I believe that when Java versions 17, 18 and higher are installed, your javac cmd will still use this older version, right?

Right. When Java is updated, the JRE (Java Runtime Environment) is updated

This automatically installs your java plugin (used in the browser) and your java command is installed in the latest version. (Open a terminal and enter the java version)

The SDK (Java SDK) that contains javac is not modified. You must do it manually.

One option is what mhaller says.

Next, I use environment variables in Windows.

MyPC / RigthClick / Properties / Advaced / EnvironmentVariables /

From there I will add JAVA_HOME to the desired JDK path (c: \ jsdk_x_x_x) and set the PATH variable to:

  whatever;_it_had;_before;%JAVA_HOME%\bin 

Is there a way to install this to automatically use the latest installed java update?

For JRE this will be automatically set, for the SDK you only need to change the system variables and change the value of JAVA_HOME

+5
Nov 06 '09 at 22:27
source share

Instead, use the variable set by the Java installer:

 set path="%path%;%JAVA_HOME%\bin" 
+2
Nov 06 '09 at 22:12
source share

The next version of the Powershell script works fine for this:

 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) 

Why use the following user-defined functions inspired here (you can include everything in the same UpdateJavaPath.ps1 script):

 <# .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 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.") } } 
0
Nov 05 '15 at 19:37
source share



All Articles