Search code examples
windowsbatch-filejava-home

batch script to check if the Java home is present in the system and to check the java version


Is there any way to write aa batch script to check whether java home is present in environment and to check whether java version is above 7

@echo off
IF "%JAVA_HOME%" == "" (
    echo Enter path to JAVA_HOME: 
    set /p JAVA_HOME=
) ELSE (
    echo %JAVA_HOME%
)

I used the above script to check to check the java home.But it is not giving any proper output


Solution

  • edited 2017/11/16 to adapt to new JRE registry location. Original answer under the line

    @echo off 
        setlocal enableextensions disabledelayedexpansion
    
        :: for variables
        ::    %%L = possible locations under HKLM\SOFTWARE of JavaSoft registry data
        ::          note: the list includes native and redirected locations
        ::    %%J = possible locations under %%L where to find the JRE information
        ::          note: the list is sorted from newer to older locations
        ::    %%k = The generated JRE root key to check
        ::    %%v = current java version
        ::    %%e = path to java    
    
        for %%L in ( 
            "" "Wow6432Node\" 
        ) do for %%J in ( 
            "JRE" "Java Runtime Environment"
        ) do if not defined javaDir (
            for %%k in (
                "HKLM\SOFTWARE\%%~LJavaSoft\%%~J"
            ) do for /f "tokens=3" %%v in (
                'reg query "%%~k" /v "CurrentVersion" 2^>nul ^| find /i "CurrentVersion"'
            ) do for /f "tokens=2,*" %%d in (
                'reg query "%%~k\%%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%"
        )
    
        pause
    

    Original answer

    if defined JAVA_HOME (
        echo JAVA_HOME="%JAVA_HOME%"
    ) else (
        echo JAVA_HOME is not defined 
    )
    

    But this will not tell you (nor will asking the user) if java is installed or what version it is.

    If java is installed, you can ask windows (it is stored in the registry) where it is and what version you have

    @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