How To Set JAVA_HOME, MAVEN_HOME Environment Variable In MacOS

After you install jdk or maven in MacOS, you may want to set JAVA_HOME or MAVEN_HOME environment variable like in windows, but you always do not know how to do it. This article will tell you how to do in MacOS or Linux.

1. Set JAVA_HOME Environment Variable In MacOS Steps.

  1. First, all environment variable settings is saved in current user’s .bash_profile file, this file is saved in current user home directory. Run cd ~ in terminal to go to current user home directory.
  2. Then execute ls -l .bash_profile to make sure that file is exist. 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, click esc key first then click i 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. Run this command in 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

2. Set MAVEN_HOME Environment Variable In MacOS Steps.

It is similar with JAVA_HOME system variable settings, but you need to install maven first.

  1. Go to https://maven.apache.org/download.cgi to download the latest maven archive file.
    apache maven download page
  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 in keyboard then enter i to insert below export command in .bash_profile.
    export MAVEN_HOME="/Users/zhaosong/Documents/WorkSpace/tool/apache-maven-3.5.4"
    
  3. Click esc key in keyboard then enter :wq in vim to save and quit vim editor.
    vim edit java_home maven_home environment variable correct
  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 terminal, now you can echo JAVA_HOME and MAVEN_HOME in terminal.
  6. Besides MAVEN_HOME, you also need to set M2_HOME, M2 and MAVEN_OPTS environment variable just like below in user .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 below error.
    192:~ zhaosong$ mvn -v
    -bash: mvn: command not found
  8. This is because you do not add $M2_HOME/bin folder in $PATH environment variable.
  9. Use vim to edit .bash_profile again and add $M2_HOME/bin in the $PATH. Please note, export PATH environment variable after $M2_HOME, otherwise PATH do not contain $M2_HOME/bin folder.
    PATH="/Library/Frameworks/Python.framework/Versions/3.6/bin:${PATH}:/Users/zhaosong/Documents/WorkSpace/tool/spring-2.0.2.RELEASE/bin:$M2_HOME/bin"
    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 http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html 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"

Reference

  1. Create Java Project With Maven
(Visited 272 times, 2 visits today)
READ :   How To Resolve Windows Remote Desktop Clipboard ( Copy & Paste ) Not Working

Leave a Reply

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.