1

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 Answer 1

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
)
Sign up to request clarification or add additional context in comments.

5 Comments

@user3400717 do you have SETX as a command on your system? (just type setx in the command prompt)
@user3400717 how does your PATH variable looks like?
"c:\Program Files\java\jre7" (path which java installed). @ npocmaka
@user3400717 - Really sorry here /:-| . My first script harmed your path variable - it does not allow quotes in it.Could you repair it through the environment variables and set it to `C:\Windows\system32\;C:\Windows` (if your windows installation is under C:)
Hi npocmaka the quotes are given here fore clarity nd no need of qoutes in the script .I have replaced it but script is not still working

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.