How to manage multiple Java JDK versions on Mac OS
In this short article, I will explain how I manage multiple Java JDK versions on Mac OS
Introduction
I am a full-time Software Developer which means I run multiple projects on my machine and each project may require a different version of Java JDK to compile or build. Similarly, different versions of Node are required for running different projects. However, in the case of Node, I use Node Version Manager (NVM) to manage multiple versions. The setup I will explain runs perfectly fine on my machine which is a MacBook Pro with Mac OS Monterey Version 12.2.
The Problem
One of my client's projects (Project A) is developed using Facebook's React-Native and it requires Java JDK 8 to compile for the Android version. It cannot compile on any Java JDK that is higher than Java JDK 8 because of the Gradle version installed in the application when it was created. Another project (Project B) which was developed in Java requires me to use Java JDK 14 or above, this is where the issue occurs. It is important to note that I am not running the React-Native project using Android Studio, instead I am using the following command:
react-native run-android
The Basics to Understand
When I ran into this issue, I had installed Java JDK 14 to run (Project B) and that compiled without any errors, however, when I tried to run (Project A) it would throw a Java error, I understood that the issue came from the Java JDK version upgrade. Hence, I went ahead and reinstalled Java JDK 8 by downloading it from Oracle's official website. After installing Java JDK 8 I tried to run the command.
Java -version
I still got the output stating Version is Java 14 which meant that even though I had installed Java JDK 8, the machine default was still set to Java JDK 14. After diving deeper into Java JDK docs for Mac OS, I came across an article that stated that Mac OS will use the latest installed version of Java JDK as the system default. This meant that I need to uninstall Java JDK 14 and then reinstall every time I want to run which was a tedious process.
The Solution
I have broken down the solution into steps for easy understanding:
Step 1 - Bash/Zsh Profile
The first step is to understand your bash profile setup. If you are using Zsh, then go ahead and run this command to see your Zsh profile:
nano ~/.zshrc
If you see that there is only one line that is taking the source from your bash profile then that means if you change your bash profile it should reflect on Zsh too. That is usually the case.
Step 2 - Setting a default Java JDK version in Bash Profile
To select a default Java JDK version it is essential to understand where these JDKs are stored. Upon installation Java JDKs are stored under the following path:
/Library/Java/JavaVirtualMachines
You can simply use the Finder Go-To Shortcut by tapping CMND+SHIFT+G keys. Then paste in the path above. This will show you a bunch of folders each holding a different version of Java JDK, you may only have one if you only have installed only one specific version of Java JDK on your machine.
Now go ahead and run the following command in the terminal to view your bash profile:
nano ~/.bash_profile
At the topmost line of your bash profile go ahead and add this line and change "PREFERRED_JAVA" to the JDK version you want to run, you can copy and paste the exact folder name found under /Library/Java/JavaVirtualMachines with the preferred version of your choice, remember to add .jdk in the line above at the end of the folder name:
export JAVA_HOME=/Library/Java/JavaVirtualMachines/PREFERRED_JAVA.jdk/Contents/Home
In my case, Java 8 was stored under the jdk1.8.0_202 folder so I added the line like so:
export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_202.jdk/Contents/Home
Step 3 - Final Step
Once you have added the JAVA_HOME path to your bash profile go ahead and save your changes by tapping CNTRL+X then type Y to confirm saving changes & finally click enter to confirm the file name.
Now go ahead and quit all terminal instances, then open a fresh terminal window and check your Java JDK version by running the following command if you chose Java 8:
java -version
If you chose a newer version of Java than Java 8 then run the following command:
java --version
You can now go ahead and change the PREFERRED_JAVA to a different version when you require to run a different version, it is necessary to restart the terminal after making changes to the bash profile so that your changes can reflect.
Conclusion
That is all folks, after running the java version command you should see the version number that you had set in your bash profile as the path. It is important to note that this is the solution I used in my setup, there may be a better way. For example, I came across some solutions that suggested using homebrew with cask, however, I could not get that set up to work on my machine. This is my first article, so if I am wrong anywhere please feel free to correct me.