MacOS Set Java Home, Maven Home, M2, Maven Opts Example

On macOS set java_home, maven_home is not as easy as in windows after you install JDK or maven. Do not worry, this article will tell you how to do it in macOS.

1. Set JAVA_HOME ( Mac ).

  1. First, all environment variable settings are saved in the current user’s .bash_profile file, this file is saved in the current user’s home directory. Run cd ~ in terminal to go to the current user home directory.
  2. Then execute ls -l .bash_profile to make sure the file exists. You can also run ls -al to list all files under your home directory.
  3. Execute vim .bash_profile command in terminal and add below export command in it. In vim, press the esc key first then press i key to insert text, click esc then :wq to save and quit.
    export JAVA_HOME="$(/usr/libexec/java_home)"
  4. Why this command in macOS? This is because $(/usr/libexec/java_home) can return the real JDK installation home directory. Run this command in the terminal like below, you can get the real JDK installation directory.
    192:~ zhaosong$ $(/usr/libexec/java_home)
    -bash: /Library/Java/JavaVirtualMachines/jdk1.8.0_172.jdk/Contents/Home: is a directory
  5. But if you really know your JDK installed directory, you can add the JDK directory in the export command in .bash_profile.
    export JAVA_HOME=/Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk/Contents/Home
  6. Then add $JAVA_HOME/bin in the value of the system environment variable PATH like below.
    export PATH=$PATH:$JAVA_HOME/bin
  7. Do not forget to run $ source .bash_profile to make the changes take effect.

2. Set MAVEN_HOME ( Mac ).

Set MAVEN_HOME on mac is similar to set JAVA_HOME on mac, but you need to install maven first.

  1. Go to to download the latest maven archive file.
  2. Unzip the zip file to a local folder ( for example /Users/zhaosong/Documents/WorkSpace/tool/apache-maven-3.5.4 ). In vim click esc key on the keyboard then press i key to insert the below export command in .bash_profile.
    export MAVEN_HOME="/Users/zhaosong/Documents/WorkSpace/tool/apache-maven-3.5.4"
  3. Press esc key on the keyboard then press :wq in vim to save and quit vim editor.
  4. Now when you run echo $JAVA_HOME, or echo $MAVEN_HOME, you still can not get the value you just set in .bash_profile.
  5. To make the change take effect, run source .bash_profile command in the terminal, now you can echo JAVA_HOME and MAVEN_HOME‘s value on the macOS terminal.
  6. Besides set MAVEN_HOME on mac, you also need to set M2_HOME, M2, and MAVEN_OPTS on mac just like below in the user’s .bash_profile.
    export JAVA_HOME="$(/usr/libexec/java_home)"
    export M2_HOME="/Users/zhaosong/Documents/WorkSpace/tool/apache-maven-3.5.4"
    export MAVEN_HOME=$M2_HOME
    export M2=$M2_HOME/bin
    export MAVEN_OPTS="-Xms256m -Xmx512m"
  7. Now when you run mvn -v, you may encounter the below error.
    192:~ zhaosong$ mvn -v
    -bash: mvn: command not found
  8. This is because you do not set $M2_HOME/bin folder in $PATH environment variable.
  9. Use vim to edit .bash_profile again and add $M2_HOME/bin in $PATH. Please note, export PATH environment variable after $M2_HOME, otherwise PATH do not contain $M2_HOME/bin folder.
    export PATH
  10. Execute source .bash_profile to make the change take effect.
  11. Run mvn -v again, another error occurred. This is because you do not install jdk correctly in your MacOS.
    192:~ zhaosong$ mvn -v
    The JAVA_HOME environment variable is not defined correctly
    This environment variable is needed to run this program
    NB: JAVA_HOME should point to a JDK not a JRE
  12. Go to to download the latest jdk8 installer and install it correctly.
  13. Now run java -version and mvn -v, you can get the correct output as below.
    192:~ zhaosong$ java -version
    java version "1.8.0_172"
    Java(TM) SE Runtime Environment (build 1.8.0_172-b11)
    Java HotSpot(TM) 64-Bit Server VM (build 25.172-b11, mixed mode)
    192:~ zhaosong$ mvn -v
    Apache Maven 3.5.4 (1edded0938998edf8bf061f1ceb3cfdeccf443fe; 2018-06-18T02:33:14+08:00)
    Maven home: /Users/zhaosong/Documents/WorkSpace/tool/apache-maven-3.5.4
    Java version: 1.8.0_172, vendor: Oracle Corporation, runtime: /Library/Java/JavaVirtualMachines/jdk1.8.0_172.jdk/Contents/Home/jre
    Default locale: en_CN, platform encoding: UTF-8
    OS name: "mac os x", version: "10.13.6", arch: "x86_64", family: "mac"

3. Run Maven With Multiple JDK Version.

  1. When you run the maven command to build a java project on mac, it gets the JDK version from mac JAVA_HOME variable.
  2. But if there are multiple JDK versions installed on your machine, and you want to use different JDK version to build different java project, You can use the maven compiler plugin to let each maven project use its own JDK.
  3. Edit your maven project pom.xml file, and add the below XML in it.
  4. You can also set mac JAVA_HOME in ~/.mavenrc file ( if the file does not exist, create ~/.mavenrc in your home folder ) like below, then maven will use this JDK when building a java project. This way you do not need to change the mac JAVA_HOME on the OS level.
    export JAVA_HOME=`/usr/libexec/java_home -v 1.9.0`
  5. Now you can run mvn -version and java -version to check that maven uses a different JDK version than the macOS does.
    # From below output we can see maven use JDK 1.9 which is configured in ~/.mavenrc file.
    $ ./mvn -version
    Apache Maven 3.6.3 (cecedd343002696d0abb50b32b541b8a6ba2883f)
    Maven home: /Users/songzhao/Downloads/apache-maven-3.6.3
    Java version: 1.9., vendor: AdoptOpenJDK, runtime: /Library/Java/JavaVirtualMachines/adoptopenjdk-9.jdk/Contents/Home/jre
    Default locale: en_US, platform encoding: UTF-8
    OS name: "mac os x", version: "10.15.7", arch: "x86_64", family: "mac"
    # Below is mac OS used JDK version.
    $ java -version
    openjdk version "1.8.0_212"
    OpenJDK Runtime Environment (AdoptOpenJDK)(build 1.8.0_212-b03)
    OpenJDK 64-Bit Server VM (AdoptOpenJDK)(build 25.212-b03, mixed mode)


  1. Create Java Project With Maven

1 thought on “MacOS Set Java Home, Maven Home, M2, Maven Opts Example”

Leave a Comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.