X
  • Tips
    December 13, 2008

Switch between different JDK versions in Windows

Guest Author

java.pngBeing 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!

Join the discussion

Comments ( 7 )
  • SwitchBL8 Saturday, December 13, 2008
    Your note is very true, so make sure you don't run more than one batch file in the same command-window. Close the command window and open a new one first.
  • Roman Rubio Monday, December 15, 2008
    why don´t use te JAVA_HOME variable for setting the PATH variable?
    Like this:
    @echo off
    echo Setting JAVA_HOME
    set JAVA_HOME=C:\Archivos de programa\Java\jdk1.6.0_07
    echo setting PATH
    set PATH=%JAVA_HOME%\bin;%PATH%
    echo Display java version
    java -version
    This way we reduce the risks you mention
  • Trevor Torrez Tuesday, November 17, 2009
    :: On modern versions of windows (XP, probably 2000, not sure about 98) - drop this script into any java installation folder:
    :: drive and path of this script
    set CWD=%~dp0
    :: drop trailing '\' when setting JAVA_HOME
    set JAVA_HOME=%CWD:~0,-1%
    :: put first in PATH
    set PATH=%JAVA_HOME%\bin;%PATH%
    :: display version
    java -version
  • Sreehareesh KM Wednesday, January 20, 2010
    It works. But immediately after changing the environment variables, they are getting reset to older values. ie issuing java -version command in a new prompt shows the old values.
  • Tanna Reeher Wednesday, April 21, 2010
    Good to be visiting your site again, it has been weeks for me. Anyway, this is the post that i've been waiting for so long. I can use this article to finish my assignment in the college, and it has same topic like your post. Regards, great share.
  • Michael Hoffer Sunday, November 24, 2013

    A while ago I have written a small tool that allows to switch between JDK versions. Link to JSelect: http://mihosoft.eu/?p=649 It works on Linux, Mac OS and Windows


  • guest Monday, February 24, 2014

    thank you! it works for me


Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.