Changing JAVA_HOME in cmd temporarily doesn't change PATH temporarily in windows

35,653

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

Share:
35,653

Related videos on Youtube

the1derer
Author by

the1derer

Google Summer of Code Intern'19 @ Checker Framework | Linux Geek | ❤️ Java

Updated on September 18, 2022

Comments

  • the1derer
    the1derer over 1 year

    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

  • the1derer
    the1derer over 5 years
    but on restarting the temporary value of JAVA_HOME will be lost
  • DavidPostill
    DavidPostill over 5 years
    You have to set it permanently and then change it back when you are done.
  • DavidPostill
    DavidPostill over 5 years
    Answer updated.
  • Dush
    Dush about 5 years
    I have already tried the method shown here before coming to this page since I visited the referenced link before. Unfortunately this didn't work. I am using Window 7 Enterprise. And also I tried this PowerShell method dzone.com/articles/using-multiple-versions-jdk. No luck.
  • DavidPostill
    DavidPostill about 5 years
    @dush Then you are doing something wrong. Please ask a new question.
  • Dush
    Dush about 5 years
    @DavidPostill, My question is almost the same. Therefore I started it over again, cut and paste above code in jdk16.bat and changing only JAVA_HOME and PATH to jdk version folder and its bin. My only difference is I have jdk1.7 an jdk1.8. Same result.