How to Set JAVA_HOME on MAC

In this tutorial, I am going to share with you how to set the JAVA_HOME environment variable on a new MAC. The new way of setting JAVA_HOME environment variable allows us to dynamically identify and use the current Java version installed on a computer as well as let us switch between Java versions if there is more than one installed.

Starting from Mac OS 10.5 there is a very convenient command that we can use to identify the installed Java version. For example, open the terminal window on your Mac and type the following command to print out the top Java version installed on your Mac

/usr/libexec/java_home

If you have more than one version installed and you want to see all of the versions available, then run the following command in the terminal window.

/usr/libexec/java_home -V

To print the current Java version installed, simply use

java -version

Set JAVA_HOME Environment Variable

To set the JAVA_HOME environment variable for a current user you will need to update the ~/.profile file. I will use the vi text editor on Mac but you can use the text editor of your choice.

  1. Open the terminal windows and type:
    sudo vi ~/.profile

    This will open the .profile file for editing.

  2. Add the following line
    export JAVA_HOME=$(/usr/libexec/java_home)

    The /usr/libexec/java_home will return the current version of Java installed on the Mac computer.

  3. Save the file by tapping on ESC button on the keyboard and then :wq. This will save the file and will quite.
  4. In the terminal window type
    source ~/.profile

    to apply changes to the .profile file right away.

  5. In the terminal window type
    java -verion

    This will print out the current Java version installed on your Mac computer.

Switch Between Java Versions

If you have more than one Java version installed on your computer and you need to switch to using a different version, you can do so by adding the -v <Java Version> flag.

Let’s have a look at a short example.

To learn what Java versions I have installed on my Mac computer I will type the following in the terminal window:

/usr/libexec/java_home -V

this will return

Matching Java Virtual Machines (2):
    13.0.1, x86_64:	"Java SE 13.0.1"	/Library/Java/JavaVirtualMachines/jdk-13.0.1.jdk/Contents/Home
    1.8.0_231, x86_64:	"Java SE 8"	/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home

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

Which tells that there are two Java versions installed and the top version is “jdk-13.0.1.jdk“.

To set a specific Java version I will need to:

  1. In the terminal window type
    sudo vi ~/.profile

    To open the .profile file for editing.

  2. Add Java version I want to set. Like so:
    export JAVA_HOME=$(/usr/libexec/java_home -v 1.8)

    or if I wanted to set Java version 13, I would do it this way

    export JAVA_HOME=$(/usr/libexec/java_home -v 13)
  3. Save the file by tapping on ESC button on your keyboard and then :wq. This will save the file and will quite.
  4. Apply changes by typing
    source ~/.profile

Now if you check Java version by typing

java -version

It should print the current Java version set in JAVA_HOME.

I hope this tutorial was helpful to you.

 

2 Comments on "How to Set JAVA_HOME on MAC"


  1. Hi, I found you tutorial extremely helpful however I made a mistake and have no experience with his kind of editing. I accidentally hit enter when trying to switch versions of java instead of esc :wq and now can’t delete the string of text “export JAVA_HOME=$(/usr/libexec/java_home- 1.8)” can you help me? Thank you.

    Reply

Leave a Reply

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

Free Video Lessons

Enter your email and stay on top of things,

Subscribe!