1

I've wroten a script to set java homepath/path to use for switching between different versions of java.

I want only want to set path if it doesn't already contains the location string to java's bin folder. As it is now, it adds to path multiple times if i run the script multiple times. How can i acchieve this?

Code

@echo off  
echo Setting JAVA_HOME  
setx -m JAVA_HOME "C:\Program Files\Java\jdk1.8.0"
echo JAVA_HOME: %JAVA_HOME% 
echo setting PATH
setx -m PATH "%Path%;%JAVA_HOME%\bin"
echo PATH: %PATH%  
echo Display java version  
java -version  
pause

Can i use if condition somehow?

Thank you...

EDIT: If i already set a java_homepath and path to java 7 jdk and in the script will set it to java jdk 8 it sets the java_homepath but when setting the path it uses the old java homepath so i need to run the script twice. Why is that and how can this be fixed?

EDIT2:

@echo off  
echo Setting JAVA_HOME  
setx -m JAVA_HOME "C:\Program Files\Java\jdk1.7.0"
echo JAVA_HOME: %JAVA_HOME% 
echo setting PATH
for /f "tokens=* delims=" %%a in ("%JAVA_HOME%\bin") do (
  if "%%~dpnfs$PATH:a" EQU "" (
   setx -m PATH "%Path%;%JAVA_HOME%\bin"
   PATH %PATH%;"%JAVA_HOME%\bin"
  )
)
echo PATH: %PATH%  
echo Display java version  
java -version  
pause

With this code it sets java_home but not path (because it uses old java_home), also it does not pause the cmd or displays java version after for loop...

Henrik
  • 1,797
  • 4
  • 22
  • 46

3 Answers3

2
echo %path%|find "%JAVA_HOME%\bin" >nul || setx -m PATH "%Path%;%JAVA_HOME%\bin"

write it (echo), check if it contains the string (find), don't write to screen (>nul) and if not found (||), set the variable.

Stephan
  • 53,940
  • 10
  • 58
  • 91
  • Thank you. btw, if i already set a java_homepath and path to java 7 jdk and in the script will set it to java jdk 8 it sets the java_homepath but when setting the path it uses the old java_homepath so i need to run the script twice. Why is that ? And how can this be fixed? – Henrik Apr 13 '14 at 12:00
  • two possibilties: first (and most probably), what @npocmaka said: "setx will not affect current cmd session", second: if you are running that piece of code in a `(`block`)`, you will (additional to "first") need delayed expanison. – Stephan Apr 13 '14 at 14:42
  • I've tried add Another PATH... after that line but it doesnt work. :( – Henrik Apr 13 '14 at 16:04
1
@echo off  
echo Setting JAVA_HOME
set "JAVA_HOME=C:\Program Files\Java\jdk1.7.0"  
setx -m JAVA_HOME "%JAVA_HOME%"
echo JAVA_HOME: %JAVA_HOME% 
echo setting PATH
    for /f "tokens=* delims=" %%a in ("%JAVA_HOME%\bin") do (
      if "%%~dpnfs$PATH:a" EQU "" (
       setx -m PATH "%JAVA_HOME%\bin;%Path%"
       PATH "%JAVA_HOME%\bin";%PATH%
      )
    )

setx will not affect current cmd session so you need additional call of PATH command

EDIT:

@echo off
for /f tokens^=2-5^ delims^=.-_^" %%j in ('java -fullversion 2^>^&1') do @set "jver=%%j%%k%%l"
set "JAVA_HOME=C:\Program Files\Java\jdk1.7.0"
if %jver% GTR 179 (
           setx -m PATH "%JAVA_HOME%\bin;%Path%"
           set "PATH="%JAVA_HOME%\bin";%PATH%"
)
npocmaka
  • 55,367
  • 18
  • 148
  • 187
  • can you please explain `"%%~dpnfs$PATH:a"`? – Stephan Apr 13 '14 at 13:30
  • 1
    ` %~$PATH:I - searches the directories listed in the PATH environment variable and expands %I to the fully qualified name of the first one found. If the environment variable name is not defined or the file is not found by the search, then this modifier expands to the empty string %~dp$PATH:I - searches the directories listed in the PATH environment variable for %I and expands to the drive letter and path of the first one found.` - from FOR /? – npocmaka Apr 13 '14 at 13:54
  • 1
    Thank you, @npocmaka. Ok, I should have had a look into the help before asking ;) I was confused by the `$` and the `:a` – Stephan Apr 13 '14 at 14:37
  • Thanks for your answer. That code doesn't work thou, it runs so fast so i cant see exception and it doesnt run my echos or pause at all. – Henrik Apr 13 '14 at 15:19
  • @Henrik - it works on windows 8 and XP for me with java home not and in the path .Could you set `PAUSE` at the end of your script to see the error? – npocmaka Apr 13 '14 at 18:32
  • @Henrik - I saw the error in the posted code it should be %java_home%\bin instead of %java_home\bin% it is now fixed – npocmaka Apr 13 '14 at 18:34
  • Thank you, now it sets java_home but not the path. It still uses the old java_home and does not add jdk 7 location to the path if jdk 8 path exists. – Henrik Apr 14 '14 at 06:34
  • @Henrik 1.You can set `SET JAVA_HOME=C:\JDK8` (or store it in another variable) at the beginning of the script to be sure that the location will point to the JDK8. 2. You can set the new location at the beginning of the path to be sure that the new `java.exe` will be used -> `setx -m PATH "%JAVA_HOME%\bin";%Path%;` and `PATH "%JAVA_HOME%\bin";%PATH%` – npocmaka Apr 14 '14 at 06:50
  • If the java_home and path is set to jdk 8 and i want to change to jdk 7 with the script, it sets java_home to jdk 7 but it doesn't add it to path because jdk8 exists in the path, so it uses the old java_home to set path. I edited my post with my code. Thanks for the help man. – Henrik Apr 14 '14 at 07:02
  • @Henrik - then set java_home to c:\jdk7 (with set command) at the beginning of the script , and again set the new java_home at the beginning of the path (I'll edit my answer too) . You can additional check for the java version too -> http://stackoverflow.com/questions/17714681/get-java-version-from-batch-file – npocmaka Apr 14 '14 at 07:12
  • It didnt affect path anyways, still uses the old java_home to set path. So if jdk 8 exists in the path and im running the script to change to jdk 7, it Changes java_home but not path. – Henrik Apr 14 '14 at 07:25
  • If you already ran the script the new JDK should be set to the path and wont change it.And if the JDK8\bin directory is before the JDK7\bin in the the PATH the`java -version` will print the 1.8.* .May be it will be better to check the java version and if it is not 7 to set JDK7\bin at the fornt of the path – npocmaka Apr 14 '14 at 07:47
  • I mean thatif java_home and path is set to jdk 8 Before i run the script. And in the script want to switch to jdk 7. It does set java_home to jdk 7, but dont add to path because jdk 8 exists in the path (therefore i assume it uses the old java_home) and therefore not add it to the path variable. IF i run the script twice, then it adds it to path. – Henrik Apr 14 '14 at 08:28
0

I did this script to toggle between java versions. Run it as Administrator. For java 10 changing the path doesn't work anymore because it ignore the JAVA_HOME and path, must uninstall 10 otherwise cannot downgrade.

If the final path string is longer than 1024 it will not work (setX limitation), try to delete "unnecessary" things from path.

@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\jdk1.9.0_24"
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