Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using jabba to Switch JDK Versions Globally on Windows #860

Open
xiangdyzz opened this issue Jul 26, 2024 · 0 comments
Open

Using jabba to Switch JDK Versions Globally on Windows #860

xiangdyzz opened this issue Jul 26, 2024 · 0 comments

Comments

@xiangdyzz
Copy link

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant