Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save harishkannarao/0a1ecfcf14f4e463ea58ca1ca26540ea to your computer and use it in GitHub Desktop.

Select an option

Save harishkannarao/0a1ecfcf14f4e463ea58ca1ca26540ea to your computer and use it in GitHub Desktop.
Windows Powershell / Terminal user function to switch Java JDK

Windows Powershell Commands

Check if profile exists

Test-Path $PROFILE

Create profile if it doesn't exist

New-Item -Type File -Path $PROFILE -Force

Edit the profile using notepad

notepad $PROFILE

Add a function to set java 25

function set-java-25 {
  $env:JAVA_HOME = "C:\Users\windows\Tools\OpenJDK25U-jdk_x64_windows_hotspot_25.0.2_10\jdk-25.0.2+10"
  $env:Path = $env:JAVA_HOME + "\bin;" + $env:Path
}

Add a function to set java 21

function set-java-21 {
  $env:JAVA_HOME = "C:\Users\windows\Tools\OpenJDK21U-jdk_x64_windows_hotspot_21.0.10_7\jdk-21.0.10+7"
  $env:Path = $env:JAVA_HOME + "\bin;" + $env:Path 
}

Change the execution policy to execute user functions

Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser

Reload the profile to test the user functions

. $PROFILE

Remove the profile if the user functions is no longer needed

Remove-Item $PROFILE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment