8

I hava defined JAVA_HOME to jdk11 in users' environment variable and PATH contains %JAVA_HOME%/bin in it.

Now I want to switch to jdk8 temporarily in command-prompt (cmd) by changing JAVA_HOME to jdk8 but it doesn't change value in path or java -version doesn't change in current instance of cmd.

P.S.- I have deleted c:\Program Data\Oracle\java.. files to be avoid any confusion

DavidPostill
  • 162,382
Shubham Raj
  • 83
  • 1
  • 1
  • 4

3 Answers3

15

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 -version

jdk15.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 -version

jdk16.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 -version

Make sure you assign the appropriate JAVA_HOME value 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_HOME and the path to java must always refer to the exact same version of the JDK. If you mix them up, unpredictable things will happen!

Source Switch between different JDK versions in Windows | Oracle Pranav's Blog

dStulle
  • 113
DavidPostill
  • 162,382
0

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;...
RalfFriedl
  • 1,734
0

I know the answer is a bit late but the accepted one did not work for me so I changed the code a bit and ended up with the following solution which worked for me:

@echo off
echo Setting JAVA_HOME
setx -m JAVA_HOME "C:\Java\jdk-1.7.0_80"
echo setting PATH
setx -m PATH "%JAVA_HOME%\bin;%PATH%"
echo Display java version
java -version

setx -m changes the system variable but If you need to change the user variable just leave out this parameter. As the script needs to be executed with administrator privileges I stored the script at the %user_home% location and created a shortcut to it. The shortcut can be placed wherever you want. One thing you need to change at the shortcut properties: Right-click the shortcut -> properties -> shortcut tab -> advanced -> run as administrator. This solution worked perfect for me.