How to get java version in batch and echo something if empty out or not

batch filejavawindows 10windows 7

this is a batch to detect java, but always set ok

setlocal EnableDelayedExpansion
for /f "tokens=3" %%i in ('java -version 2^>^&1 ^| findstr /i "version"') do (echo %%i)
IF %%i == "" (echo fail) else (echo ok)

How do I modify this script so that if java is not installed on the system (32 or 64 bits or both) "echo FAIL" and if it is installed (32 or 64 bits or both) "echo OK"

PD: If it doesn't work properly with the above, I would also accept responses with this command (or any other):

wmic product where "name like '%%java%%' AND NOT Name like '%%java auto%%'"

PD: I have found a batch that does just what I want, but it is long and complicated, and I have not been able to simplify it. I make it available to give solution ideas

Best Answer

For the case you describe, you should use the where command coupled with checking the exit code (ERRORLEVEL) to see if the program exists and is in the path.

Example:

    @echo off
    where java 2>NUL
    if "%ERRORLEVEL%"=="0" (call :JavaFound) else (call :NoJava)
    goto :EOF

    ::::::::::::::::::::::::::::::::::::::::::::
    :JavaFound
    ::::::::::::::::::::::::::::::::::::::::::::
    Echo Java found.. checking version...
    goto :EOF

    ::::::::::::::::::::::::::::::::::::::::::::
    :NoJava
    ::::::::::::::::::::::::::::::::::::::::::::
    Echo Java not found, you need to install it.
    :: install java here?
    goto :EOF
Related Question