How to set JAVA_HOME environment variable on Mac OS?

I have installed a newer version of Java on my machine but Maven keeps on using the older version of Java because echo $JAVA_HOME is still pointing to the older installation. How do I update JAVA_HOME environment variable on my Mac OS X.

  1. Accepted Answer

Accepted Answer

Using Bash or zsh

First, find the directory where your Java SDK is installed. The examples below assume it is in /usr/libexec/java_home

You’ll need to know which shell program you are running. If you are not sure, run ps -p $$ and it will tell you the shell that you have.

If you are using bash, run the following command to set the $JAVA_HOME env variable.

echo export "JAVA_HOME=\$(/usr/libexec/java_home)" >> ~/.bash_profile

If you are using zsh, run the following command to set the $JAVA_HOME env variable.

echo export "JAVA_HOME=\$(/usr/libexec/java_home)" >> ~/.zshrc

Next, you must restart your terminal –or– use the source command for the changes to take affect. If you are using the source, use the flavor depending on your shell:

  • bash: source ~/.bash_profile
  • zsh: source ~/.zshenv

Speak Your Mind