20

I'm working on few projects and some of them are using different JDK. Switching between JDK versions is not comfortable. So I was wondering if there is any easy way to change it?

I found 2 ways, which should solve this problem, but it doesn't work.

First solution is creating a bat files like this:

@echo off
echo Setting JAVA_HOME
set JAVA_HOME=C:\Program Files\Java\jdk1.7.0_72
echo setting PATH
set PATH=C:\Program Files\Java\jdk1.7.0_72\bin;%PATH%
echo Display java version
java -version
pause

And after running this bat, I see right version of Java. But when I close this CMD and open a new one and type "java -version" it says that I still have 1.8.0_25. So it doesn't work.

Second solution which I found is an application from this site. And it also doesn't work. The same effect as in the first solution.

Any ideas? Because changing JAVA_HOME and PAHT by: Win + Pause -> Advanced System Settings -> Environment Variables -> and editing these variables, is terrible way...

Lui
  • 414
  • 2
  • 8
  • 17
  • Set path with batch file, and execute java too inside batch file. – brb tea Nov 18 '14 at 11:26
  • 1
    As you can see both variables are setting in batch file. But I don't know how to execute java inside this file. I found this solution on another page and I don't know how to write batch files. – Lui Nov 18 '14 at 11:30

4 Answers4

27

The set command only works for the current terminal. To permanently set a system or user environment variable you can use setx.

setx JAVA_HOME "C:\Program Files\Java\jdk1.7.0_72" /m

The /m option is used to set the variable system wide (and not just for the current user). The terminal must be run as administrator to use this option.

The variable will be available in all new terminal windows, but not the current one. If you want to display the path in the same window, you need to use both set and setx.

You can avoid manipulating the PATH variable if you just once put %JAVA_HOME% in there, instead of the full JDK path. If you change JAVA_HOME, PATH will be updated too.


There are also a few environment variable editors as alternative to the cumbersome Windows environment variable settings. See "Is there a convenient way to edit PATH in Windows 7?" on Super User.

kapex
  • 26,163
  • 5
  • 97
  • 111
  • 1
    Hmmm something is wrong... I did as you said and it adds these variables, but for user... And still when I open new terminal I get info that I have different JDK than I want... – Lui Nov 18 '14 at 11:45
  • 1
    It was missing the /m option, I've updated the answer. – kapex Nov 18 '14 at 12:04
  • Do I also have to replace the second "set" with "setx" (line: set PATH=C:\Program Files\Java\jdk1.7.0_72\bin;%PATH%)? – OddDev Jul 27 '15 at 08:19
  • @OddDev If you permanently want to change the PATH, you need to use `setx` there too. It will add the java path every time you run the script though, so it may be better to remove that line and to add `%JAVA_HOME%` to the PATH only once – kapex Jul 27 '15 at 12:50
  • Terminal or application needs a restart to take into account changes in `PATH` caused by updated `JAVA_HOME`. Or https://stackoverflow.com/questions/171588/is-there-a-command-to-refresh-environment-variables-from-the-command-prompt-in-w – Konstantin Spirin Sep 01 '20 at 22:22
3

In case if someone want to switch frequently in each new command window then I am using following approach.

Create batch file using below code. You can add n number of version using if and else blocks.

@echo off
if "%~1" == "11" (
   set "JAVA_HOME=C:\Software\openjdk-11+28_windows-x64_bin\jdk-11"
) else (
   set "JAVA_HOME=C:\Program Files\Java\jdk1.8.0_151"
)
set "Path=%JAVA_HOME%\bin;%Path%"
java -version

Save this batch file as SJV.bat and add this file location in your machine's Path environment variable. So now SJV will act as command to "Switch Java Version".

Now open new command window and just type SJV 11 it will switch to Java 11. Type SJV 8 it will switch to Java 8.

I hope this help someone who want to change it frequently.

Prakash Boda
  • 148
  • 10
1

If your path have less than 1024 characters can execute (Run as Administrator) this script:

@echo off 
set "JAVA5_FOLDER=C:\Java\jdk1.5.0_22"
set "JAVA6_FOLDER=C:\Java\jdk1.6.0_45"
set "JAVA7_FOLDER=C:\Java\jdk1.7.0_80"
set "JAVA8_FOLDER=C:\Java\jdk1.8.0_121"
set "JAVA9_FOLDER=C:\Java\jdk-10.0.1"
set "CLEAR_FOLDER=C:\xxxxxx"

(echo "%PATH%" & echo.) | findstr /O . | more +1 | (set /P RESULT= & call exit /B %%RESULT%%)
set /A STRLENGTH=%ERRORLEVEL%
echo path length = %STRLENGTH%
if %STRLENGTH% GTR 1024  goto byebye 

echo Old Path: %PATH%
echo =================== 
echo Choose new Java Version:
echo [5] JDK5
echo [6] JDK6 
echo [7] JDK7
echo [8] JDK8
echo [9] JDK10
echo [x] Exit

:choice 
SET /P C=[5,6,7,8,9,x]? 
for %%? in (5) do if /I "%C%"=="%%?" goto JDK_L5 
for %%? in (6) do if /I "%C%"=="%%?" goto JDK_L6
for %%? in (7) do if /I "%C%"=="%%?" goto JDK_L7 
for %%? in (8) do if /I "%C%"=="%%?" goto JDK_L8 
for %%? in (9) do if /I "%C%"=="%%?" goto JDK_L9
for %%? in (x) do if /I "%C%"=="%%?" goto byebye
goto choice 

@echo on
:JDK_L5  
set "NEW_PATH=%JAVA5_FOLDER%"
goto setPath

:JDK_L6  
@echo off 
set "NEW_PATH=%JAVA6_FOLDER%"
goto setPath

:JDK_L7  
@echo off 
set "NEW_PATH=%JAVA7_FOLDER%"
goto setPath

:JDK_L8  
@echo off 
set "NEW_PATH=%JAVA8_FOLDER%"
goto setPath

:JDK_L9  
@echo off 
set NEW_PATH = %JAVA9_FOLDER%

:setPath
Call Set "PATH=%%PATH:%JAVA5_FOLDER%=%CLEAR_FOLDER%%%"
Call Set "PATH=%%PATH:%JAVA6_FOLDER%=%CLEAR_FOLDER%%%"
Call Set "PATH=%%PATH:%JAVA7_FOLDER%=%CLEAR_FOLDER%%%"
Call Set "PATH=%%PATH:%JAVA8_FOLDER%=%CLEAR_FOLDER%%%"
Call Set "PATH=%%PATH:%JAVA9_FOLDER%=%CLEAR_FOLDER%%%"
rem echo Interim Path: %PATH%
Call Set "PATH=%%PATH:%CLEAR_FOLDER%=%NEW_PATH%%%" 

setx PATH "%PATH%" /M

call set "JAVA_HOME=%NEW_PATH%"
setx JAVA_HOME %JAVA_HOME% 

echo New Path: %PATH%
:byebye
echo
java -version
pause

If more than 1024, try to remove some unnecessary paths, or can modify this scripts with some inputs from https://superuser.com/questions/387619/overcoming-the-1024-character-limit-with-setx

1
  1. Open Environment Variables editor (File Explorer > right click on This PC > Properties > Advanced system settings > Environment Variables...)
  2. Find Path variable in System variables list > press Edit > put %JAVA_HOME%bin; at first position. This is required because Java installer adds C:\Program Files (x86)\Common Files\Oracle\Java\javapath to the PATH which references to the latest Java version installed. enter image description here
  3. Now you can switch between Java version using setx command (should be run under administrative permissions):

    setx /m JAVA_HOME "c:\Program Files\Java\jdk-10.0.1\
    

    (note: there is no double quote at the end of the line and should not be or you'll get c:\Program Files\Java\jdk-10.0.1\" in your JAVA_HOME variable and it breaks your PATH variable)

Solution with system variables (and administrative permissions) is more robust because it puts desired path to Java at the start of the resulting PATH variable.

Lu55
  • 17,620
  • 4
  • 65
  • 60