0

I have a Windows batch file which checks for an installed java product.

Unfortunately, the problem is that after an error on the initial java -version command, the errorlevel value is set to 9009, and after that in the reg query command, the error level does not reset to 0, and The [Step2] error message is displayed, even value exists in the registry.

I tried to change the errorlevel value, but it is not working.

Any Idea? Is this possible?

Here is my batch script:

@echo off
setlocal EnableExtensions DisableDelayedExpansion

echo #################################    START JAVA TEST     #################################
java -version
if %ERRORLEVEL% GTR 0 (
    echo:
    echo [Step1]: Test JAVA failed !!!
    ::- Get the Java Version
    set ERRORLEVEL = 0
    set KEY="HKEY_LOCAL_MACHINE\SOFTWARE\JavaSoft\JDK"
    set KEY64="HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\JavaSoft\Java Development Kit"
    set KEY2="HKLM\SOFTWARE\JavaSoft\Java Runtime Environment"
    set KEY264="HKLM\SOFTWARE\WOW6432Node\JavaSoft\Java Runtime Environment"
    set VALUE=CurrentVersion
    reg query %KEY% /v %VALUE% 2>nul || (
        set KEY=!KEY64!
        reg query !KEY! /v %VALUE% 2>nul || (
            set KEY=!KEY2!
            reg query !KEY! /v %VALUE% 2>nul || (
                set KEY=!KEY264!
                reg query !KEY! /v %VALUE% 2>nul || (
                    echo [Step2]: Perhaps the JDK 1.8 or higher runtime package is not installed.
                    echo [Step2]: Please try to install JDK 1.8 or higher and set the JAVA_HOME Variable in Windows
                    echo #################################     End JAVA TEST      ###################################
                    echo:
                    pause
                    exit /b 1
                )
            )
        )
    )

    set JAVA_VERSION=
    for /f "tokens=2,*" %%a in ('reg query %KEY% /v %VALUE% ^| findstr %VALUE%') do (
        set JAVA_VERSION=%%b
    )
    echo [Found]: JAVA VERSION: %JAVA_VERSION%
    ::- Get the JavaHome
    set KEY="HKEY_LOCAL_MACHINE\SOFTWARE\JavaSoft\JDK\%JAVA_VERSION%"
    set KEY64="HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\JavaSoft\Java Development Kit\%JAVA_VERSION%"
    set KEY2="HKLM\SOFTWARE\JavaSoft\Java Runtime Environment\%JAVA_VERSION%"
    set KEY264="HKLM\SOFTWARE\WOW6432Node\JavaSoft\Java Runtime Environment\%JAVA_VERSION%"
    set VALUE=JavaHome
    set ERRORLEVEL = 0
    reg query %KEY% /v %VALUE% 2>nul || (
        set KEY=!KEY64!
        reg query !KEY! /v %VALUE% 2>nul || (
            set KEY=!KEY2!
            reg query !KEY! /v %VALUE% 2>nul || (
                set KEY=!KEY264!
                reg query !KEY! /v %VALUE% 2>nul || (
                    echo [Step3]: JavaHome not installed
                    echo [Step3]: Please try to install JDK 1.8 or higher and set the JAVA_HOME Variable in Windows
                    echo #################################     End JAVA TEST      ###################################
                    echo:
                    pause
                    exit /b 1
                )
            )
        )
    )
    set JAVAHOME=
    for /f "tokens=2,*" %%a in ('reg query %KEY% /v %VALUE% ^| findstr %VALUE%') do (
        set JAVAHOME=%%b
    )
    echo [INFO]: JavaHome Found in %JAVAHOME%
    echo [INFO]: Trying to set Java Path to System Environment
    setx -m PATH "%Path%;%JAVAHOME%\bin"
    set errorlevel=0
    echo [INFO]: Java Path set in System Environment
    echo [INFO]: Testing Java Version
    java -version
    if %ERRORLEVEL% GTR 0 (
        echo:
        echo [Step4]: Test JAVA failed !!!
        echo [Step4]: Please set the System Environment Path Manually
        echo:
        echo #################################    End JAVA TEST     ###################################
        echo Please run install.bat again after installing runtime!
        echo:
        pause
        exit /b 1
    )
    echo [INFO]: Java Version working fine!
)

echo [Installation Script]: Test for the Java successfully passed. Good!
echo #################################     End JAVA TEST      ###################################
echo:

endlocal
Compo
  • 36,585
  • 5
  • 27
  • 39
Hamid
  • 378
  • 3
  • 8
  • 2
    Also applicable (because I see you're using delayed expansion even though you've explicitly disabled it): [Windows Batch Variables Won't Set](https://stackoverflow.com/questions/9681863/windows-batch-variables-wont-set) – SomethingDark Jan 25 '21 at 14:29
  • [`ERRORLEVEL` is not `%ERRORLEVEL%`](https://devblogs.microsoft.com/oldnewthing/?p=20743); the variable you set to 0 will never be *greater* than 0, no matter what exit codes commands are returning. – Jeroen Mostert Jan 25 '21 at 14:32
  • 2
    Within a block statement `(a parenthesised series of statements)`, `REM` statements rather than the broken-label remark form (`:: comment`) should be used because labels terminate blocks, confusing `cmd`. – Magoo Jan 25 '21 at 14:55

0 Answers0