Fix default java version on Maven on Mac OS X

Etimbuk U
1 min readJul 3, 2018

--

Recently installed Java10 to see whats new and fun about it. Once I did this and tried running existing projects which use java8.

To my surprise, maven began using java10 as its default java version, even though my JAVA_HOME is set to use java8 — /usr/libexec/java_home -v 1.8

So confused and confounded, I turn to Googling…

From Googling, I learn maven runs using latest java installation. Now this is configured in M2_HOME/bin/mvn

Given, my installation was done using brew — Brew is simply a package manager for Mac OS, my M2_HOME was automatically set up. This is usually in /usr/local/Cellar/maven/3.5.4

With this new found knowledge, I run

nano /usr/local/Cellar/maven/3.5.4/bin/mvn

The content was

#!/bin/bashJAVA_HOME="${JAVA_HOME:-$(/usr/libexec/java_home)}" exec "/usr/local/Cellar/maven/3.5.4/libexec/bin/mvn" "$@"

Important bit being

"${JAVA_HOME:-$(/usr/libexec/java_home)}"

To resolve this, you will need to specify the java version you need maven to default to. So in my case, I needed java8.

Update /usr/local/Cellar/maven/3.5.4/bin/mvnfile as follows

#!/bin/bashJAVA_HOME="${JAVA_HOME:-$(/usr/libexec/java_home -v 1.8)}" exec "/usr/local/Cellar/maven/3.5.4/libexec/bin/mvn" "$@"

Now run mvn -v, you should see maven uses java8 as its default JDK.

--

--

Etimbuk U
Etimbuk U

Written by Etimbuk U

Java | Flutter | Android | Some JavaScript | API Design

Responses (2)