Change the Java version in macOS

Photo by Thoa Ngo on Unsplash

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 🍎☕.

1️⃣ Step

Go to the Users and right-click on the <PROFILE_NAME> folder, and open a terminal window from here.

2️⃣ Step

Type

/usr/libexec/java_home -V

and check the available java versions.

Also by typing,

java -version

you can check your current java version.

Now we are at an intersection.

by giphy

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 🍎

ℹ️ Step

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

/Library/Java/JavaVirtualMachines/jdk-11.0.15.1.jdk/Contents/Home.

ℹ️ℹ️ Step

Type

open -t .zprofile

in the terminal window and replace the JAVA_HOME variable value with the copied path. For example,

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

Now save the file and close it. This will permanently change the java version on your device. You can confirm it by typing

java -version

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.

by giphy

Temporarily change the java version ☕

ℹ️ Step

Type

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 17.0.3.1`

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 😉.

Conclusion ✌

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! 👋👨‍💻👩‍💻👨‍💻

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store