Using cod开发者_运维百科e, 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:
- 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.
I think perhaps you're interested in calling these:
System.getProperty("os.name");
System.getProperty("java.version");
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).
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.
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.
精彩评论