4

When I do mvn --v, I get:

Apache Maven 3.0.5
Maven home: /usr/share/maven
Java version: 1.6.0_45, vendor: Sun Microsystems Inc.
Java home: /usr/lib/jvm/java-6-oracle/jre
Default locale: en_IN, platform encoding: UTF-8
OS name: "linux", version: "3.16.0-30-generic", arch: "amd64", family: "unix"

When I do java -version, I get:

java version "1.7.0_76"
Java(TM) SE Runtime Environment (build 1.7.0_76-b13)
Java HotSpot(TM) 64-Bit Server VM (build 24.76-b04, mixed mode)

Now I want maven to use java 7, how do I make this so?

coder005
  • 153
  • 1
  • 2
  • 9

3 Answers3

8

Firstly I opened Ubuntu Software center and searched for jdk removed the installer and default jdk 6 from there. Then I run the command :

sudo update-alternatives --config java
sudo update-alternatives --config javac

And selected the openjdk-7 from there. But when I run :

mvn --v

it said that JAVA_HOME is not set. Then did the following steps:

sudo gedit /etc/bash.bashrc

Added the following lines at the end of the file :

export JAVA_HOME=/usr/lib/jvm/java-7-oracle
export PATH=$PATH:$JAVA_HOME/bin

Then close the gedit. And run the command :

source /etc/bash.bashrc
echo $JAVA_HOME

Path was set. And now :

mvn -v
Apache Maven 3.0.5
Maven home: /usr/share/maven
Java version: 1.7.0_76, vendor: Oracle Corporation
Java home: /usr/lib/jvm/java-7-oracle/jre
Default locale: en_IN, platform encoding: UTF-8
OS name: "linux", version: "3.16.0-30-generic", arch: "amd64", family: "unix"
coder005
  • 153
  • 1
  • 2
  • 9
  • Also see this Answer: http://askubuntu.com/questions/315646/update-java-alternatives-vs-update-alternatives-config-java/388693#388693 You can use update-java-alternatives to update both compiler and runtime – Benjamin Maurer Feb 05 '15 at 18:48
3

To set the JAVA_HOME variable for only maven you can override your defaults by setting it in your $HOME/.mavenrc file.

The $HOME/.mavenrc is sourced by the mvn script. This is useful if you want to switch jvm for different projects.

This is the content of my $HOME/.mavenrc file:

#export JAVA_HOME=/usr/lib/jvm/java-1.7.0-openjdk-amd64
#export JAVA_HOME=/usr/lib/jvm/java-7-oracle
export JAVA_HOME=/usr/lib/jvm/java-8-oracle

This way i can comment out the jvm i want to use before i run maven.

stalet
  • 589
  • 4
  • 13
0

Here is the documentation on Ubuntu Environment variables.

What we want to do here is persist our JAVA_HOME path.

Most commonly, our java installations go in /usr/lib/jvm. To list the installations present,

ls /usr/lib/jvm

I have /usr/lib/jvm/java-8-openjdk-amd64 among them and that is installation I want to use for maven.

We can add our entry for JAVA_HOME entry to ~/.pam_environment and make it DEFAULT or OVERRIDE - that's your call.

Use whatever text editor you want to open the file (I am using a text editor that is lying around in my system - it's name is xed. You may use gedit or vim or anything)

xed ~/.pam_environment

You will find things like:

enter image description here

Now, let us add a new entry for our JAVA_HOME environment variable. In a new line, enter:

JAVA_HOME   DEFAULT=<your path to your java home location>

or

JAVA_HOME   DEFAULT=${JAVA_HOME}    OVERRIDE=<path to your java home location>

As my java home location is '/usr/lib/jvm/java-8-openjdk-amd64', I am entering JAVA_HOME DEFAULT=${JAVA_HOME} OVERRIDE=/usr/lib/jvm/java-8-openjdk-amd64

Now, log out of your system and log back in for the change to take effect.

Let's check the maven version now.

mvn -version

and it should work : enter image description here

ThunderBird
  • 1,955