Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to determine if the Java VM is installed on Windows?

Tags:

java

windows

jvm

Using code, how to determine the Java VM is installed(and it version) in Windows .

like image 460
user496949 Avatar asked Dec 01 '22 08:12

user496949


1 Answers

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:

  • There are two redirections to the nul device, one for standard output and one for standard error.
  • The detection is done separately from the value parsing to avoid showing an ERROR... message when the value does not exist.
  • There is a space character after the delims= option (since space is the delimiter).
  • The batch file returns an error level / exit code of zero if successful (installed) or 1 on failure (not installed).

Hope it helps.

like image 51
devstuff Avatar answered Dec 05 '22 10:12

devstuff