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/
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.
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
If you are in Linux, set JAVA_HOME using syntax export JAVA_HOME=<path-to-java>
. Actually it is not only for Maven.
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:
JAVA_HOME=/usr/lib/jvm/java-8-oracle/jre
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
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
Great helps above, but if you having the similar environment like I did, this is how I get it to work.
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