如何确定 Java VM 是否安装在 Windows 上?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5362941/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 16:27:45  来源:igfitidea点击:

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

javawindowsjvm

提问by user496949

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

使用代码,如何确定在 Windows 中安装了 Java VM(及其版本)。

回答by devstuff

Assuming you wish to programatically determine this from a batch file, you can use the reg.exetool, installed in windows\system32.

假设您希望通过批处理文件以编程方式确定这一点,您可以使用reg.exe安装在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.

这个工具的烦人之处在于无法让它只返回一个退出代码,所以你必须通过重定向到无处来抑制它的输出。当该值不存在时,它也会生成一个 ERROR 消息。

@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 nuldevice, 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).
  • 设备有两种重定向nul,一种用于标准输出,一种用于标准错误。
  • 检测与值解析分开进行,以避免ERROR...在值不存在时显示消息。
  • delims=选项后有一个空格字符(因为空格是分隔符)。
  • 如果成功(安装)或失败(未安装),批处理文件返回错误级别/退出代码为零。

Hope it helps.

希望能帮助到你。

回答by WhiteFang34

I think perhaps you're interested in calling these:

我想也许你有兴趣调用这些:

System.getProperty("os.name");
System.getProperty("java.version");

回答by Andrew Thompson

Oracle's deployJava.jscan 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.

Oracle 的deployJava.js不仅可以检查用户机器上可用的特定最低 Java 版本,而且如果他们在 Windows 上,还可以在需要时指导他们下载和安装它。

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).

通常 deployJava.js 用于为使用Java Web Start& applet启动的应用程序提供启动按钮,您可以使用 JWS 表单链接到“裸”Jar 文件(没有依赖项)。

回答by Michael

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.

无法通过 Java 代码检测是否安装了 JVM。如果 JVM 不存在,Java 代码就无法运行。因此,如果您能够运行已编译的代码,则 JVM 已安装。

回答by Dunaril

I don't really consider that a JVM can be installedon 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 binfolder in the PATHenvironment variable.

我真的不认为可以在 Windows上安装JVM 。我更应该检查您的硬盘驱动器目录中是否有 JRE,以及是否可以通过环境变量中的bin文件夹引用轻松访问该 JRE PATH

You can check this by trying to run java -versionfrom code and collecting the output.

您可以通过尝试java -version从代码运行并收集输出来检查这一点。

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.

如果您收到一条消息,说明'java' is not recognized as an internal or external command, operable program or batch file.,那么您的 jre 没有被引用。