How to change maven java home [duplicate]
Asked Answered
D

9

92

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.

Datha answered 6/12, 2012 at 21:3 Comment(0)
D
79

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

Daedal answered 6/12, 2012 at 21:10 Comment(3)
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/Jasminejason
Does it work only for the current Terminal window or does it change system properties?Canadian
@BCqrstoO's help in one line: export JAVA_HOME=`readlink -f \`command -v java\``Kassala
C
59

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

Cornelius answered 16/6, 2016 at 14:42 Comment(6)
Also works on Linux; it will look something like JAVA_HOME=/usr/lib/jvm/java-8-oracle/jre.Helsie
I've added the Linux part to the answer. I didn't test it. Please tell me if it works.Cornelius
Thank you, thank you, thank you. My very stupid self of 4 years ago caused me this pain that you solved. :)Outmost
Or better yet, just remove .mavenrc (or at least the line that specifies JAVA_HOME), and maven will default to your normal JAVA_HOME.Microbarograph
I wish I could upvote this 1000 times. Wasted way too many hours trying on this only to find this solution. Thank you!Feed
For me, the mavenrc was located in /etc/mavenrcWadi
P
20

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

Plowman answered 23/4, 2018 at 19:53 Comment(2)
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).Bagwig
you saved 3 days of mine... thanks !Bra
M
16

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"
Mckim answered 13/2, 2020 at 10:5 Comment(0)
T
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
Tyrrell answered 16/4, 2014 at 5:4 Comment(0)
T
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.

Talos answered 24/6, 2015 at 11:54 Comment(1)
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. ThanksMention
C
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.

Crookes answered 8/12, 2012 at 4:27 Comment(0)
C
2

Just set JAVA_HOME env property.

Coprophilous answered 6/12, 2012 at 21:6 Comment(0)
S
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
Strangles answered 18/10, 2017 at 14:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.