jabba
jabba copied to clipboard
Using jabba to Switch JDK Versions Globally on Windows
Define a function in the PowerShell profile $PROFILE to switch JDK versions globally. You can edit the $PROFILE file by running the following command in PowerShell:
notepad $PROFILE
Add the following content to the $PROFILE file:
function Set-JDK {
param (
[string]$Version
)
switch ($Version) {
"1.8" {
$jabbaVersion = "[email protected]"
}
"1.17" {
$jabbaVersion = "[email protected]"
}
default {
Write-Host "Unknown version: $Version. Please use '1.8' or '1.17'."
return
}
}
if (-not $jabbaVersion) {
Write-Host "JDK version not set."
return
}
jabba use $jabbaVersion
$jdkPath = (jabba which $(jabba current)).Trim()
if (-not $jdkPath) {
Write-Host "Unable to get JDK path. Please check the JDK version."
return
}
[Environment]::SetEnvironmentVariable('JAVA_HOME', $jdkPath, 'User')
Write-Host "JAVA_HOME environment variable set to: $jdkPath"
Write-Host "Switched to JDK version $Version ($jabbaVersion)."
}
Set-Alias jdk Set-JDK
To reload the profile and make the new function effective, run the following command in PowerShell:
. $PROFILE
Now, you can switch JDK versions using the jdk command. For example:
jdk 1.8
jdk 1.17