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:

  1. From the command prompt, run the following command:
    set JAVA_HOME=C:Program FilesJavajdk1.8.0_20
    
  2. For Linux or Mac, the command will be:
    export JAVA_HOME=<java-8-home-folder>
    
  3. Now, run the following command to check the version of Maven installed:
    mvn –version
    
  4. 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:projectsapache-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:softwareapache-maven-3.2.5
    Java version: 1.8.0_20, vendor: Oracle Corporation
    Java home: C:Program FilesJavajdk1.8.0_20jre
    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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.137.216.175