90

I want to change maven java home which is open jdk with sun jdk. How can I do it ?

root@ak-EasyNote-TM98:~# mvn -version
Apache Maven 3.0.4
Maven home: /usr/share/maven
Java version: 1.6.0_24, vendor: Sun Microsystems Inc.
Java home: /usr/lib/jvm/java-6-openjdk-amd64/jre
Default locale: tr_TR, platform encoding: UTF-8
OS name: "linux", version: "3.2.0-34-generic", arch: "amd64", family: "unix"

Edit:

So sorry. I forgot to write the below code :

root@ak-EasyNote-TM98:~$ java -version
java version "1.7.0_07"
Java(TM) SE Runtime Environment (build 1.7.0_07-b10)
Java HotSpot(TM) 64-Bit Server VM (build 23.3-b01, mixed mode)

My java home default path is sun jdk already. But maven java home has pointed openjdk. I want to fix it only for maven.

RockOnGom
  • 3,893
  • 6
  • 35
  • 53

9 Answers9

74

If you are in Linux, set JAVA_HOME using syntax export JAVA_HOME=<path-to-java>. Actually it is not only for Maven.

Evgeniy Dorofeev
  • 133,369
  • 30
  • 199
  • 275
  • 30
    For those who do not know where oracle-java is installed to, you can find this by first running `which java` which will tell you which binary of java is being called. In my case it was `/usr/bin/java`. At this point you can run `readlink -f /usr/bin/java`, which for me yielded `/usr/lib/jvm/java-7-oracle/jre/bin/java` ergo `export JAVA_HOME=/usr/lib/jvm/java-7-oracle/jre/` – Niko May 12 '14 at 14:42
  • 1
    Does it work only for the current Terminal window or does it change system properties? – Koray Tugay Jul 16 '14 at 18:19
  • 1
    @BCqrstoO's help in one line: `export JAVA_HOME=\`readlink -f \\`command -v java\\`\`` – dfarrell07 Nov 09 '16 at 22:27
58

I am using Mac and none of the answers above helped me. I found out that maven loads its own JAVA_HOME from the path specified in: ~/.mavenrc

I changed the content of the file to be: JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_45.jdk/Contents/Home

For Linux it will look something like:
export JAVA_HOME=/usr/lib/jvm/java-8-oracle/jre

inanutshellus
  • 9,683
  • 9
  • 53
  • 71
Technotronic
  • 8,424
  • 4
  • 40
  • 53
18

The best way to force a specific JVM for MAVEN is to create a system wide file loaded by the mvn script.

This file is /etc/mavenrc and it must declare a JAVA_HOME environment variable pointing to your specific JVM.

Example:

export JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-amd64

If the file exists, it's loaded.

Here is an extract of the mvn script in order to understand :

  if [ -f /etc/mavenrc ] ; then
    . /etc/mavenrc
  fi

  if [ -f "$HOME/.mavenrc" ] ; then
    . "$HOME/.mavenrc"
  fi

Alternately, the same content can be written in ~/.mavenrc

fxrobin
  • 590
  • 3
  • 9
  • Also, a warning to RHEL CentOS users: have a look at the mvn script, because it may differ from the code snippet above. In my case it has a nasty ". /etc/java/maven.conf" line between the two ifs, so if I insert a JAVA_HOME variable into /etc/mavenrc it gets overwritten in the next step. I had to edit /etc/java/maven.conf (and hope that upgrading the maven package won't overwrite my changes in the future). – Attila Csipak Mar 02 '21 at 14:31
  • you saved 3 days of mine... thanks ! – Digao Feb 04 '22 at 16:27
14

If you are dealing with multiple projects needing different Java versions to build, there is no need to set a new JAVA_HOME environment variable value for each build. Instead execute Maven like:

JAVA_HOME=/path/to/your/jdk mvn clean install

It will build using the specified JDK, but it won't change your environment variable.

Demo:

$ mvn -v
Apache Maven 3.6.0
Maven home: /usr/share/maven
Java version: 11.0.6, vendor: Ubuntu, runtime: /usr/lib/jvm/java-11-openjdk-amd64
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "4.15.0-72-generic", arch: "amd64", family: "unix"

$ JAVA_HOME=/opt/jdk1.8.0_201 mvn -v
Apache Maven 3.6.0
Maven home: /usr/share/maven
Java version: 1.8.0_201, vendor: Oracle Corporation, runtime: /opt/jdk1.8.0_201/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "4.15.0-72-generic", arch: "amd64", family: "unix"

$ export | grep JAVA_HOME
declare -x JAVA_HOME="/usr/lib/jvm/java-11-openjdk-amd64"
Jasper de Vries
  • 19,370
  • 6
  • 64
  • 102
5

Great helps above, but if you having the similar environment like I did, this is how I get it to work.

  • having a few jdk running, openjdk, oracle jdk and a few versions.
  • install apache-maven via yum, package is apache-maven-3.2.1-1.el6.noarch

Edit this file /etc/profile.d/apache-maven.sh, such as the following, note that it will affect the whole system.

$ cat /etc/profile.d/apache-maven.sh
MAVEN_HOME=/usr/share/apache-maven
M2_HOME=$MAVEN_HOME
PATH=$MAVEN_HOME/bin:$PATH
# change below to the jdk you want mvn to reference.
JAVA_HOME=/usr/java/jdk1.7.0_40/
export MAVEN_HOME
export M2_HOME
export PATH
export JAVA_HOME
Jasonw
  • 5,054
  • 7
  • 43
  • 48
4

Even if you install the Oracle JDK, your $JAVA_HOME variable should refer to the path of the JRE that is inside the JDK root. You can refer to my other answer to a similar question for more details.

Community
  • 1
  • 1
Ivaylo Slavov
  • 8,839
  • 12
  • 65
  • 108
  • 2
    Thanks. Solved my problem. If the JAVA_HOME was showing the JDK root, then Surefire has taken JDK/bin/java as JAVA_HOME for its forked JVM, which is false. Saved my day. Thanks – Gábor Lipták Sep 28 '15 at 10:06
3

Appears to be a duplicate of https://askubuntu.com/questions/21131/how-to-correctly-remove-openjdk-and-jre-and-set-the-system-use-only-and-only-sun#answer-21137 assuming that you are using Ubuntu.

The key is to use the command sudo update-java-alternatives -s java-6-sun. Any commands that rely on javac will be affected and not just Maven.

Community
  • 1
  • 1
Drew
  • 1,950
  • 3
  • 22
  • 28
2

Just set JAVA_HOME env property.

Piotr Gwiazda
  • 12,080
  • 13
  • 60
  • 91
1

I have two Java versions on my Ubuntu server 14.04: java 1.7 and java 1.8.

I have a project that I need to build using java 1.8.

If I check my Java version using java -version

I get

java version "1.8.0_144"

But when I did mvn -version I get:

Java version: 1.7.0_79, vendor: Oracle Corporation

To set the mvn version to java8

I do this:

export JAVA_HOME=/usr/lib/jvm/java-8-oracle/jre/

Then when I do mvn -version I get:

Java version: 1.8.0_144, vendor: Oracle Corporation
halfer
  • 19,824
  • 17
  • 99
  • 186
Mubashar Abbas
  • 5,536
  • 4
  • 38
  • 49