Install Multiple Java versions and Setup Maven to Use one – Java 1.7

Problem

I was running Java 1.8 by default but I needed to build some modules that require Java 1.7

Solution

Check the default version of Java installed as well as the version of Java that maven is using:


implementer@whiscardz ~ $ java -version
java version "1.8.0_131"
Java(TM) SE Runtime Environment (build 1.8.0_131-b11)
Java HotSpot(TM) 64-Bit Server VM (build 25.131-b11, mixed mode)

implementer@whiscardz ~ $ mvn -v
Java HotSpot(TM) 64-Bit Server VM warning: ignoring option MaxPermSize=512m; support was removed in 8.0
Apache Maven 3.3.9
Maven home: /usr/share/maven
Java version: 1.8.0_131, vendor: Oracle Corporation
Java home: /usr/lib/jvm/java-8-oracle/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "4.4.0-78-generic", arch: "amd64", family: "unix" 
  • Install Java 1.7 as instructed in this link.
  • Create a .mavenrc file in your home directory and then add the JAVA_HOME of Java 1.7 In my case this would be┬áJAVA_HOME=/usr/lib/jvm/java-7-oracle/jre
implementer@whiscardz ~ $ vim .mavenrc

JAVA_HOME=/usr/lib/jvm/java-7-oracle/jre
  • Optional, Run the maven file to assign the JAVA variable on the current terminal
implementer@whiscardz ~ $ source .mavenrc
  • Check version of Java Maven is now using:
implementer@whiscardz ~ $ mvn -version
Apache Maven 3.3.9
Maven home: /usr/share/maven
Java version: 1.7.0_80, vendor: Oracle Corporation
Java home: /usr/lib/jvm/java-7-oracle/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "4.4.0-78-generic", arch: "amd64", family: "unix"

References

Create .mavenrc file

How to change maven java home