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

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

3 Comments

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?
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.
I've tried add Another PATH... after that line but it doesnt work. :(
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%"
)

13 Comments

can you please explain "%%~dpnfs$PATH:a"?
` %~$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 /?
Thank you, @npocmaka. Ok, I should have had a look into the help before asking ;) I was confused by the $ and the :a
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 - 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?
|
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.