Changing the Java version on your device is essential for almost all developers nowadays as there are multiple Long Term Support(LTS) java versions available. In this tutorial, we will see an one way on how to change the java version in macOS 🍎☕.
Go to the Users and right-click on the <PROFILE_NAME> folder, and open a terminal window from here.
and check the available java versions.
Also by typing,
you can check your current java version.
Now we are at an intersection.
First, we will look at how we are going to change the java version permanently and then we will see how we are going to change the java version temporarily.
Permanently change the java version 🍎
Copy the location part of your desired java version, which was obtained from step 2. For example, if I want to use java 11 as the permanent java version, the path will be
open -t .zprofile
in the terminal window and replace the JAVA_HOME variable value with the copied path. For example,
Now save the file and close it. This will permanently change the java version on your device. You can confirm it by typing
in a new terminal window.
Now let’s look at how we can temporarily change the java version. This only changes the java version for that opened terminal window. When you close that window or open a new terminal window, it will use your permanent java version.
Temporarily change the java version ☕
export JAVA_HOME=`/usr/libexec/java_home -v <DESIRED JAVA VERSION>`
in the terminal window. The exact version name of your desired java version can be found using the command in step 2. An example is shown below,
export JAVA_HOME=`/usr/libexec/java_home -v 22.214.171.124`
This will change your java version only for the currently opened terminal. This will come in handy when you try to test your project compatibility with various java versions 😉.
This is the end of the article. Before I finish I want to say, if I have made any mistakes or if there is an easy way to do this please add a comment below about it. If you find this article useful, feel free to share this with your friends and colleagues.
Thank you and HAPPY CODING! 👋👨💻👩💻👨💻