Skip to content

Instantly share code, notes, and snippets.

@holmeszyx
Last active March 30, 2026 01:41
Show Gist options
  • Select an option

  • Save holmeszyx/b60e34d7264dccb7f95eba11c72ab45c to your computer and use it in GitHub Desktop.

Select an option

Save holmeszyx/b60e34d7264dccb7f95eba11c72ab45c to your computer and use it in GitHub Desktop.
Swith java version in PowerShell. Saving the script and Appending '. "C:\Path\To\Your\Script\switch-java.ps1"' to "$PROFILE" file. For git-bash, put 'test -f ~/bin/switch-java.sh && . ~/bin/switch-java.sh' to '.bash_profile'
#!/usr/bin/env bash
# Git-Bash edition of Java version switcher
# Usage: source this file in your .bashrc, then call `switch-java <version>` or `sj <version>`
# e.g. sj 21
switch-java() {
local version="$1"
# Java installation paths (git-bash style)
declare -A java_paths=(
[8]="/c/Programs/dev/java/java"
[11]="/c/Programs/dev/java/java11"
[17]="/c/Programs/dev/java/java17"
[21]="/c/Programs/dev/java/java21"
)
# Validate version
if [[ -z "${java_paths[$version]+_}" ]]; then
echo "Invalid Java version. Available versions: ${!java_paths[*]}"
return 1
fi
local java_home="${java_paths[$version]}"
# Remove all known Java paths from PATH
local new_path="$PATH"
for p in "${java_paths[@]}"; do
# Remove both "<path>/bin" and "<path>" entries
new_path=$(echo "$new_path" | tr ':' '\n' | grep -v -F "$p" | paste -sd ':' -)
done
# Prepend new Java bin path
export PATH="${java_home}/bin:${new_path}"
# Set JAVA_HOME (Windows-style for tools that expect it)
local java_home_win
java_home_win=$(cygpath -w "$java_home")
export JAVA_HOME="$java_home_win"
# Set CLASSPATH
export CLASSPATH=".;${java_home_win}\\lib"
echo "JAVA_HOME set to: $JAVA_HOME"
echo "PATH updated to include: ${java_home}/bin"
echo "CLASSPATH set to: $CLASSPATH"
echo ""
java -version
}
# Alias
alias sj='switch-java'
function Switch-Java {
param (
[string]$Version
)
# The location of multi-version java
$javaPaths = @{
"8" = "C:\Programs\dev\java\java"
"11" = "C:\Programs\dev\java\java11"
"17" = "C:\Programs\dev\java\java17"
}
# check input
if (-not $javaPaths.ContainsKey($Version)) {
Write-Host "Invalid Java version. Available versions: 8, 11, 17"
return
}
# new java home
$javaHome = $javaPaths[$Version]
# set java home
[System.Environment]::SetEnvironmentVariable("JAVA_HOME", $javaHome, [System.EnvironmentVariableTarget]::Process)
# old path
$currentPath = [System.Environment]::GetEnvironmentVariable("PATH", [System.EnvironmentVariableTarget]::Process)
# remove outdated java path
$newPath = ($currentPath -split ';' | Where-Object {
$javaPaths.Values -notcontains $_ -and $javaPaths.Values -notcontains ($_ -replace '\\bin$', '')
}) -join ';'
# new path
$newPath = "$javaHome\bin;$newPath"
[System.Environment]::SetEnvironmentVariable("PATH", $newPath, [System.EnvironmentVariableTarget]::Process)
# classpath
$classPath = ".;$($javaHome)\lib"
[System.Environment]::SetEnvironmentVariable("CLASSPATH", $classPath, [System.EnvironmentVariableTarget]::Process)
# print result JAVA_HOME\PATH\CLASSPATH
Write-Host "JAVA_HOME set to: $javaHome"
Write-Host "PATH updated to include: $javaHome\bin"
Write-Host "CLASSPATH set to: $classPath"
Write-Host ""
# java now
java -version
}
# alias
Set-Alias -Name sj -Value Switch-Java
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment