If you don't have admin rights, use the below command to set environment variables for java using the command prompt
setx JAVA_HOME "C:\Program Files\Java\jdk1.8.0"
setx PATH "%PATH%;%JAVA_HOME%\bin";
Modify the environment variable.
setx -m JAVA_HOME "C:\Program Files\Java\jdk1.8.0"
setx -m PATH "%PATH%;%JAVA_HOME%\bin";
Answer from Sathiamoorthy on Stack Overflowjava adding to path
Trouble changing Java version on Windows
Help making a script to switch between Java JDK Versions (changing JAVA_HOME path)
I installed the latest version of java, but when I type "java --version" on terminal it shows the old one.
If you don't have admin rights, use the below command to set environment variables for java using the command prompt
setx JAVA_HOME "C:\Program Files\Java\jdk1.8.0"
setx PATH "%PATH%;%JAVA_HOME%\bin";
Modify the environment variable.
setx -m JAVA_HOME "C:\Program Files\Java\jdk1.8.0"
setx -m PATH "%PATH%;%JAVA_HOME%\bin";
Set the JAVA_HOME Variable
Windows 7 – Right click My Computer and select Properties > Advanced
Windows 8 – Go to Control Panel > System > Advanced System Settings
Windows 10 – Search for Environment Variables then select Edit the system environment variables

Click the Environment Variables button.
Under System Variables, click New.
In the Variable Name field, enter either:
JAVA_HOME if you installed the JDK (Java Development Kit)
or
JRE_HOME if you installed the JRE (Java Runtime Environment). In the Variable Value field, enter your JDK or JRE installation path .

Open Command Prompt as Administrator.
Set the value of the Environment variable to your JDK (or JRE) installation path as follows:
setx -m JAVA_HOME "C:\path_to_Java\jdk_version"
It doesn't change value in path or java -version doesn't change in current instance of cmd
You have to restart cmd for the changes to take effect as a cmd shell inherits it environment from the parent process.
So what is the correct way to switch between Java versions from the command line?
Use a set of batch files, as follows:
Being a Java developer, I always compile and test my code on different Java versions. But switching between them is a huge problem. So finally I found an easy method to do this. You have to create following batch files and place them in directory you open your command line in or in SYSTEM PATH. You can use you favorite text editor to create these files.
jdk14.bat
@echo off echo Setting JAVA_HOME set JAVA_HOME=C:\j2sdk1.4.2_12 echo setting PATH set PATH=C:\j2sdk1.4.2_12\bin;%PATH% echo Display java version java -versionjdk15.bat
@echo off echo Setting JAVA_HOME set JAVA_HOME=C:\Program Files\Java\jdk1.5.0_12 echo setting PATH set PATH=C:\Program Files\Java\jdk1.5.0_12\bin;%PATH% echo Display java version java -versionjdk16.bat
@echo off echo Setting JAVA_HOME set JAVA_HOME=C:\Program Files\Java\jdk1.6.0_11 echo setting PATH set PATH=C:\Program Files\Java\jdk1.6.0_11\bin;%PATH% echo Display java version java -versionMake sure you assign the appropriate
JAVA_HOMEvalue in batch files, according to your Java installation. Whenever you want to switch between Java versions, just run the respective batch file and you are done.Note:
JAVA_HOMEand the path to java must always refer to the exact same version of theJDK. If you mix them up, unpredictable things will happen!
Source Switch between different JDK versions in Windows | Oracle Pranav's Blog
The reason for this is that the variable reference in PATH is expanded at the time of the assignment to PATH, any later changes are ignored, the reference to the original value is lost. It's like making a copy of the value of the variable, not creating a reference to the variable.
set JAVA_HOME=C:\dir1
PATH=%JAVA_HOME%
set JAVA_HOME=C:\dir2
PATH
This will output C:\dir1 (value of JAVA_HOME at the time of the assignment) and not %JAVA_HOME%.
You need a script like this to call after a change to JAVA_HOME:
PATH=%JAVA_HOME%;C:\Windows\system32;C:\Windows;...