Using code, how to determine the Java VM is installed(and it version) in Windows .
Assuming you wish to programatically determine this from a batch file, you can use the reg.exe
tool, installed in windows\system32
.
The annoying thing about this tool is that there is no way to have it return only a exit code, so you have to suppress its output via redirection to nowhere. And it also generates an ERROR message when the value does not exist.
@echo off
rem
rem DetectJvmInstalled.cmd
rem
reg.exe query "HKLM\Software\JavaSoft\Java Runtime Environment" /v "CurrentVersion" > nul 2> nul
if errorlevel 1 goto NotInstalled
rem Retrieve installed version number.
rem The reg.exe output parsing found at http://www.robvanderwoude.com/ntregistry.php
set JvmVersion=
for /F "tokens=3* delims= " %%A IN ('reg.exe query "HKLM\Software\JavaSoft\Java Runtime Environment" /v "CurrentVersion"') do set JvmVersion=%%A
rem if "%JvmVersion%" == "" goto NotInstalled
:Installed
echo JVM Version = %JvmVersion%
exit /b 0
:NotInstalled
echo JVM Not installed.
exit /b 1
Things to note:
nul
device, one for standard output and one for standard error.ERROR...
message when the value does not exist.delims=
option (since space is the delimiter).Hope it helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With