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 OverflowVideos
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"
Find JDK Installation Directory
First you need to know the installation path for the Java Development Kit.
Open the default installation path for the JDK:
CopyC:\Program Files\Java
There should be a subdirectory like:
CopyC:\Program Files\Java\jdk1.8.0_172
Note: one has only to put the path to the jdk without /bin in the end (as suggested on a lot of places). e.g. C:\Java\jdk1.8.0_172 and NOT C:\Java\jdk1.8.0_172\bin !
Set the JAVA_HOME Variable
Once you have the JDK installation path:
- Right-click the My Computer icon on your desktop and select Properties.
- Click the Advanced tab, then click the Environment Variables button.
- Under System Variables, click New.
- Enter the variable name as JAVA_HOME.
- Enter the variable value as the installation path for the Java Development Kit.
- Click OK.
- Click Apply Changes.
Note: You might need to restart Windows
The complete article is here, on my blog: Setting JAVA_HOME Variable in Windows.
What worked for me was adding the %JAVA_HOME%\bin to the Path environment variable with the JAVA_HOME environment variable pointing to the jdk folder.
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;...