Set Java Version On MacOS High Sierra

While working with Ionic and PhoneGap or let’s say anything related to Cordova, I ran into an error requiring Java JDK 1.8 which is required to run the build command. It took a bit of searching to find a concise explanation of how this is done on Mac OSx High Sierra.

The Steps

First find the proper Java Development Kit which is a little confusing because the original error referred to installing JDK 1.8 which which is actually JDK8. Install the following through the standard .dmg method. I found the download here:

Next you want to see which version(s) of Java you have available, you can do this with the following command which returned the two versions listed below for me. By default I was using the 9.0.4 version.

/usr/libexec/java_home -V
Matching Java Virtual Machines (2):
9.0.4, x86_64: "Java SE 9.0.4" /Library/Java/JavaVirtualMachines/jdk-9.0.4.jdk/Contents/Home
1.8.0_161, x86_64: "Java SE 8" /Library/Java/JavaVirtualMachines/jdk1.8.0_161.jdk/Contents/Home

Once we have these listed, we can use the following to install the version we want to use. Notice the use of the version listed above. Also notice that the statement uses the back tick not a single quote (found with the tilde).

export JAVA_HOME=`/usr/libexec/java_home -v 1.8.0_161`

Now we can run the following command to see which Java version is running on our system.

java -version

Which should return something like this:

java version "1.8.0_161"
Java(TM) SE Runtime Environment (build 1.8.0_161-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.161-b12, mixed mode)


Again, it seems pretty straight forward and it is but before some research it was still a little unclear. This will last until you reboot your computer and will revert back to the default Java version. I think this is good practice so that anything using the default version can use it and I can switch the version when I’m in code mode developing apps.

Feel free to ask questions or offer comments below!

Leave comment

Your email address will not be published. Required fields are marked with *.