问题
Hi I need a batch script to check whether java home is available in the system and script to compare the java version
My requirements are
1.) Script needs to check whether java home is set in the system.I think "echo %JAVA_HOME%" will do it.But if Java Home is not available it needs to display a message showing "Java home is not available.Please set the java home in Computer>Properties>Variables path".
2.) It needs to check the java version in the system and if the java vesion is higher than 1.6_445 it needs to diplay in a message that "Java version is higher MR tool will not able to install.(I think for this one it needs to create an array and store the Java version values and it needs to compare the values.But I dont know how to write the code for it)Since the java version contains underscore the comparison will be a tough task I think.
Currently I'm using the below script but it is not at all satisfying my requirements.Can any one please help me to make a script to satisfying above requirements.Thanks in advance
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:
@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
)
来源:https://stackoverflow.com/questions/24752202/batch-script-to-check-the-java-home-and-compare-java-version