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 ).
- 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. - Then execute
ls -l .bash_profile
to make sure the file exists. You can also runls -al
to list all files under your home directory. - 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)"
- 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
- 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
- Then add $JAVA_HOME/bin in the value of the system environment variable PATH like below.
export PATH=$PATH:$JAVA_HOME/bin
- 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.
- Go to https://maven.apache.org/download.cgi to download the latest maven archive file.
- 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"
- Press esc key on the keyboard then press :wq in vim to save and quit vim editor.
- Now when you run
echo $JAVA_HOME
, orecho $MAVEN_HOME
, you still can not get the value you just set in .bash_profile. - 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. - 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"
- Now when you run
mvn -v
, you may encounter the below error.192:~ zhaosong$ mvn -v -bash: mvn: command not found
- This is because you do not set $M2_HOME/bin folder in $PATH environment variable.
- 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.
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
- Execute
source .bash_profile
to make the change take effect. - 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
- Go to http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html to download the latest jdk8 installer and install it correctly.
- Now run
java -version
andmvn -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.
- When you run the maven command to build a java project on mac, it gets the JDK version from mac JAVA_HOME variable.
- 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.
- Edit your maven project pom.xml file, and add the below XML in it.
<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.6.2</version> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> </plugins> </build> or <properties> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> </properties> <build> <plugins> <plugin> ...... </plugin> </plugins> </build>
- 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`
- Now you can run
mvn -version
andjava -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)
Reference
I am able to see the maven version. But when I try in new terminal it’s not working