'Switching between different jdk's running in same machine

I have two jdk running in my machine one is openjdk and one is Oracle jdk. I have two bash profiles and i switch between the profiles.

I have this in .bash_profile

export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_261.jdk/Contents/Home

I have this in .bash_profile1

 export JAVA_HOME=/Library/Java/JavaVirtualMachines/temurin-8.jdk/Contents/Home

and I do source .bash_profile1 to switch between profiles, So now when i do echo $JAVA_HOME. It should return path based on the bash profile which i have sourced.

It should return

/Library/Java/JavaVirtualMachines/temurin-8.jdk/Contents/Home

But it is returning JAVA_HOME which is there in other .bash_profile

/Library/Java/JavaVirtualMachines/jdk1.8.0_261.jdk/Contents/Home.

What is the issue or Let me know if I am doing anything wrong.



Solution 1:[1]

After chaging .bash_profile you should relaod it using this command source ~/.bash_profile.

but I advice you to use sdkman tool to switch between jdk versions easily https://sdkman.io/usage

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Housni Abdellatif