Changing the JDK used by Maven
It is possible to have more than one version of JDK installed on your system. By following some simple steps, you can specify and/or change the JDK to be used by Maven.
How to do it...
You will recall that, in the earlier section, we used Java SE 7. Let us now change to Java SE 8. To change the JDK version to Java SE 8 on Microsoft Windows, perform the following steps:
- From the command prompt, run the following command:
set JAVA_HOME=C:\Program Files\Java\jdk1.8.0_20
- For Linux or Mac, the command will be:
export JAVA_HOME=<java-8-home-folder>
- Now, run the following command to check the version of Maven installed:
mvn –version
- To check the version of Maven installed on Microsoft Windows, run the following command from the command prompt. You should get the following output. The output will be similar for Linux and Mac:
C:\projects\apache-maven-cookbook>mvn -version
The output for the preceding command is shown as:
Apache Maven 3.2.5 (12a6b3acb947671f09b81f49094c53f426d8cea1; 2014-12-14T22:59:23+05:30) Maven home: C:\software\apache-maven-3.2.5 Java version: 1.8.0_20, vendor: Oracle Corporation Java home: C:\Program Files\Java\jdk1.8.0_20\jre Default locale: en_IN, platform encoding: Cp1252 OS name: "windows 8.1", version: "6.3", arch: "amd64", family: "windows"
How it works...
Maven always uses the JDK specified by JAVA_HOME
, no matter how many JDK installations are available on the system. This allows the user the flexibility to change JDKs as required or based on the project.
Hence, it is important to ensure JAVA_HOME
is defined. In the absence of this variable, Maven attempts to detect the presence of Java from PATH
. This is typically JRE and not JDK.