Using code, how to determine the Java VM is installed(and it version) in Windows .
-
1Java code can run without Java VM? – Dante May Code Mar 19 '11 at 15:17
-
Maybe OP means to detect from batch file or another language? Please explain. – mellamokb Mar 19 '11 at 15:20
-
He didn't say the code had to be Java. – Ken Mar 19 '11 at 15:32
-
1Goto control panel and chk it yourself, this belongs to Superuser.com unless more information is provided! – Narayan Mar 19 '11 at 15:48
-
Run hello world java program, if it prints hello world, then your done :) – Ant's Mar 19 '11 at 16:54
5 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.

- 8,277
- 1
- 27
- 33
I think perhaps you're interested in calling these:
System.getProperty("os.name");
System.getProperty("java.version");

- 70,765
- 18
- 106
- 111
-
2Not sure if this was what the OP was looking for, but this is what I was searching for. Thanks. – NeilMonday Sep 06 '11 at 21:14
Oracle's deployJava.js can not only check a particular minimum version of Java is available on the user's machine, but if they are on Windows, guide them through downloading and installing it if needed.
Normally deployJava.js is used to provide launch buttons for applications launched using Java Web Start & applets, you might use the JWS form to link to a 'naked' Jar file (with no dependencies).

- 1
- 1

- 168,117
- 40
- 217
- 433
There is no way to detect whether or not the JVM is installed from Java code. Java code cannot run without the JVM being present. Hence if you are able to run the compiled code the JVM is installed.

- 2,460
- 3
- 27
- 47
I don't really consider that a JVM can be installed on Windows. It would me more relevant to check whether you do have a JRE inside a directory of your hard drive, and whether this JRE is easily accessible via a reference of its bin
folder in the PATH
environment variable.
You can check this by trying to run java -version
from code and collecting the output.
If you get a message stating 'java' is not recognized as an internal or external command,
operable program or batch file.
, then your jre is not referenced.

- 2,757
- 5
- 31
- 53
-
1While Microsoft Windows still supports the PATH variable, it prefers it's registry to resolve program file names to their complete path. So just checking the entries of the PATH variable is not sufficient. Just running "java -version", as you suggested, seems to be the most reliable and easiest solution. – Hendrik Brummermann Mar 19 '11 at 15:44