This is a quick tip, in order to configure the Windows Powershell for set the Java and Maven variables when is there another Java installation on the system, but you only want to apply on the current terminal session to avoid change the system configuration.
There are two ways to do that:
1. Replacing current environment variables in session.
$env:JAVA_HOME = "C:\Program Files\Java\jdk-11"
$env:PATH = "$env:JAVA_HOME\bin;" + $env:PATH
$env:M2_HOME = "C:\Program Files\maven\apache-maven-3.5.3"
$env:PATH = "$env:M2_HOME\bin;" + $env:PATH
Execution output:
> java -version
openjdk version "11" 2018-09-25
OpenJDK Runtime Environment 18.9 (build 11+28)
OpenJDK 64-Bit Server VM 18.9 (build 11+28, mixed mode)
> mvn.cmd -version
Apache Maven 3.5.3 (3383c37e1f9e9b3bc3df5050c29c8aff9f295297; 2018-02-24T13:49:05-06:00)
Maven home: C:\Program Files\maven\apache-maven-3.5.3\bin..
Java version: 11, vendor: Oracle Corporation
Java home: C:\Program Files\Java\jdk-11
Default locale: es_419, platform encoding: Cp1252
OS name: "windows 10", version: "10.0", arch: "amd64", family: "windows"
2. Replacing current environment variables and execute with the specific Java home
$env:JAVA_HOME = "C:\Program Files\Java\jdk-11"
$M2_HOME = "C:\Program Files\maven\apache-maven-3.5.3"
$env:Path += ";"+$M2_HOME+"\bin"
$env:Path += ";"+$JAVA_HOME+"\bin"
Execution output:
> java -version
java version "1.8.0_321"
Java(TM) SE Runtime Environment (build 1.8.0_321-b07)
Java HotSpot(TM) 64-Bit Server VM (build 25.321-b07, mixed mode)
> & $env:JAVA_HOME\bin\java.exe -version
openjdk version "11" 2018-09-25
OpenJDK Runtime Environment 18.9 (build 11+28)
OpenJDK 64-Bit Server VM 18.9 (build 11+28, mixed mode)
> mvn.cmd -version
Apache Maven 3.5.3 (3383c37e1f9e9b3bc3df5050c29c8aff9f295297; 2018-02-24T13:49:05-06:00)
Maven home: C:\Program Files\maven\apache-maven-3.5.3\bin..
Java version: 11, vendor: Oracle Corporation
Java home: C:\Program Files\Java\jdk-11
Default locale: es_419, platform encoding: Cp1252
OS name: "windows 10", version: "10.0", arch: "amd64", family: "windows"
This last is very useful when you have installed different Java versions and you don’t need to change your system configuration.
In order to reset the configuration restart the terminal session.
That’s all, bye