用于检查java主目录的批处理脚本

时间:2014-07-09 10:12:09

标签: windows batch-file command java-home

我需要检查java home是否在我的系统中可用,如果java home不可用,则需要显示错误消息并且想要检查java版本是否小于1.7。如果它大于1.7它需要显示错误。这是可能的。目前我使用下面的脚本。可以任何一个建议mADe脚本需要什么更改。或者是否需要任何新脚本。 提前致谢

@echo off 
setlocal enableextensions disabledelayedexpansion

:: possible locations under HKLM\SOFTWARE of JavaSoft registry data
set "javaNativeVersion="
set "java32ON64=Wow6432Node\"

:: for variables
::    %%k = HKLM\SOFTWARE subkeys where to search for JavaSoft key
::    %%j = full path of "Java Runtime Environment" key under %%k
::    %%v = current java version
::    %%e = path to java

set "javaDir="
set "javaVersion="
for %%k in ( "%javaNativeVersion%" "%java32ON64%") do if not defined javaDir (
    for %%j in (
        "HKLM\SOFTWARE\%%~kJavaSoft\Java Runtime Environment"
    ) do for /f "tokens=3" %%v in (
        'reg query "%%~j" /v "CurrentVersion" 2^>nul ^| find /i "CurrentVersion"'
    ) do for /f "tokens=2,*" %%d in (
        'reg query "%%~j\%%v" /v "JavaHome"   2^>nul ^| find /i "JavaHome"'
    ) do ( set "javaDir=%%~e" & set "javaVersion=%%v" )
)

if not defined javaDir (
    echo Java not found
) else (
    echo JAVA_HOME="%javaDir%"
    echo JAVA_VERSION="%javaVersion%"
)

endlocal
pause

1 个答案:

答案 0 :(得分:0)

您可以使用LSSGRTEQU等来批量比较字符串。

  • EQU =等于
  • NEQ =不等于
  • LSS =少于
  • LEQ =小于或等于
  • GTR =大于
  • GEQ =大于或等于

由于您已获得%javaVersion%中的版本信息,因此您只需显示错误消息。

您可以将代码修改为:

if not defined javaDir (
    echo Java not found
) else (
    echo JAVA_HOME="%javaDir%"
    echo JAVA_VERSION="%javaVersion%"
    if "%javaVersion%" LSS "1.7" (
        echo Java version is too low!
        goto exit
    ) 
    if "%javaVersion%" GTR "1.7" (
        echo JAVA version is too high!
        goto exit
    )
)

:exit
endlocal
pause