用于设置java homepath / path的批处理脚本 - 仅添加到路径(如果尚未包含)

时间:2014-04-13 11:37:32

标签: windows batch-file

我已经编写了一个脚本来设置java homepath / path以用于在不同版本的java之间切换。

我想只想设置路径,如果它还没有包含java的bin文件夹的位置字符串。就像现在一样,如果我多次运行脚本,它会多次添加路径。我怎么能这样做呢?

代码

@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

我可以以某种方式使用if条件吗?

谢谢...

编辑:如果我已经设置了java_homepath和java 7 jdk的路径,并且在脚本中将它设置为java jdk 8它设置java_homepath但是在设置路径时它使用旧的java homepath所以我需要运行脚本两次。为什么这样,以及如何解决这个问题?

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

使用此代码设置java_home但不设置路径(因为它使用旧的java_home),也不会暂停cmd或在for循环后显示java版本...

3 个答案:

答案 0 :(得分:2)

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

写它(echo),检查它是否包含字符串(find),不要写入屏幕(>nul),如果没有找到(||} ),设置变量。

答案 1 :(得分: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不会影响当前cmd会话,因此您需要额外调用PATH命令

编辑:

@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%"
)

答案 2 :(得分:0)

我做了这个脚本来在Java版本之间切换。以管理员身份运行。对于Java 10,更改路径不再起作用,因为它会忽略JAVA_HOME和路径,必须将其卸载10,否则无法降级。

如果最终路径字符串大于1024,则它将不起作用(setX限制),请尝试从路径中删除“不必要的”内容。

@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