用于检查Java主目录并比较Java版本的批处理脚本

时间:2014-07-15 07:20:54

标签: java windows batch-file batch-processing java-home

嗨我需要一个批处理脚本来检查java home是否在系统和脚本中可用来比较java版本

我的要求是

1。)脚本需要检查系统中是否设置了java home。我认​​为“echo%JAVA_HOME%”会这样做。但是如果Java Home不可用,则需要显示一条消息“Java home not not not可以。请在计算机>属性>变量路径“。

中设置java home

2。)它需要检查系统中的java版本,如果java vesion高于1.6_445,它需要显示“Java版本更高的MR工具无法安装的消息。”我认为对于这个,它需要创建一个数组并存储Java版本值,它需要比较值。但我不知道如何为它编写代码)由于java版本包含下划线,我认为比较将是一项艰巨的任务

目前我正在使用下面的脚本,但它完全不能满足我的要求。任何人都可以帮我制作一个脚本来满足上述要求。谢谢提前

回声     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 :(得分:1)

 @echo off
:: 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
    exit /b 1
) else (
    echo JAVA_HOME=%javaDir%
    set JAVA_HOME=%javaDir%
    PATH %JAVA_HOME%\bin;%PATH%
    java.exe >nul 2>&1 || (
        setx JAVA_HOME %JAVA_HOME%
        setx PATH %PATH%
    )
    echo  JAVA_VERSION=%javaVersion%
)

for /f tokens^=2-5^ delims^=.-_^" %%j in ('java -fullversion 2^>^&1') do @set "jver=%%j%%k%%l%%m"
echo full java version %jver%
if %jver% GTR  16445 (
    echo "Java version is higher MR tool will not able to install"
    exit /b 2
)