如何确定Java VM是否已安装在Windows上? [英] How to determine if the Java VM is installed on Windows?
本文介绍了如何确定Java VM是否已安装在Windows上?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
使用代码,如何确定Windows中已安装Java VM(及其版本).
Using code, how to determine the Java VM is installed(and it version) in Windows .
推荐答案
假设您希望以编程方式从批处理文件中确定此内容,则可以使用安装在windows\system32
中的reg.exe
工具.
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
注意事项:
- 有两种重定向到
nul
设备的重定向,一种用于标准输出,一种用于标准错误. - 检测与值解析分开进行,以避免在值不存在时显示
ERROR...
消息. -
delims=
选项后有一个空格字符(因为空格是定界符). - 如果成功(安装),批处理文件返回的错误级别/退出代码为零;如果失败(未安装),则返回1的错误级别/退出代码.
- 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).
希望有帮助.
这篇关于如何确定Java VM是否已安装在Windows上?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文