Assume, you are installing multiple Java versions in macOS, so how do you change from one version to another? In this tutorial, I will guide you to do this.
First, let’s open Terminal and enter the following command to check all the Java versions that we have installed:
1 |
/usr/libexec/java_home -V |
Result:
As you can see, currently I am installing two versions of Java 8 and Java 9, the default is Java 9.
1 |
/Library/Java/JavaVirtualMachines/jdk-9.jdk/Contents/Home |
Now, I need to use Java 8 so I will switch to Java 8 as follows:
- Copy the path that is installing Java 8, here’s: /Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home
- Open the .bash_profile file located in the user directory, and then add the following line:
1 |
export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home |
- Save these changes.
For these changes to take effect immediately, you need to run the following command:
1 |
. /Users/Khanh/.bash_profile |
Result: