如何从批处理脚本检查 Java 安装?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2951804/
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-10-29 23:38:49  来源:igfitidea点击:

How to check Java installation from batch script?

javabatch-filepath

提问by Ma99uS

I need to write a batch script to find out if Java is installed, and if it is, then under what path? I feel it has to be something similar to this:

我需要写一个批处理脚本来找出是否安装了Java,如果是,那么在什么路径下?我觉得它必须与此类似:

for /f %%j in ("java.exe") do (
   set JAVA_HOME=..........
)

but I can not figure it out.

但我想不通。

P.S. It has to work with path with spaces two. Like if java is installed into "Program Files".

PS 它必须与带有空格两个的路径一起使用。就像 java 安装到“程序文件”中一样。

Thanks.

谢谢。

回答by Photodeus

Using reg[.exe] you can query possible JRE candidates that are installed on the system. There could be none or could be several.

使用 reg[.exe] 您可以查询系统上安装的可能的 JRE 候选对象。可能没有,也可能有几个。

On a test set-up, running inside the command shell:

在测试设置中,在命令外壳内运行:

reg query "HKLM\Software\JavaSoft\Java Runtime Environment"

reg query "HKLM\Software\JavaSoft\Java Runtime Environment"

I get three result lines, of which the first is CurrentVersion REG_SZ 1.6

我得到三个结果行,其中第一个是 CurrentVersion REG_SZ 1.6

Based on that, querying

基于此,查询

reg query "HKLM\Software\JavaSoft\Java Runtime Environment.6\"

Gives me JavaHome REG_SZ C:\Program Files\Java\jre6

给我 JavaHome REG_SZ C:\Program Files\Java\jre6

It's much more efficient than scan a file system to find a java binary.

这比扫描文件系统以查找 Java 二进制文件要高效得多。

This was tested under a virtual installation of Windows XP 32-bit.

这是在 Windows XP 32 位虚拟安装下测试的。

回答by javamonkey79

Couldn't you use the 'where' command? As in:

你不能使用'where'命令吗?如:

>where java

And test against this?

并对此进行测试?

Example:

例子:

C:\Users\myname>where java
C:\Program Files (x86)\Java\jdk1.6.0_17\bin\java.exe

C:\Users\myname>where foo
INFO: Could not find files for the given pattern(s).

回答by JamesG

Most recent versions write to the registry:

最新版本写入注册表:

HKEY_LOCAL_MACHINE\SOFTWARE\JavaSoft

HKEY_LOCAL_MACHINE\SOFTWARE\JavaSoft

You can look for what keys are in there and get out the path using reg.exe

您可以查找其中的键并使用reg.exe找出路径

回答by Federico Destefanis

I would (using batch)

我会(使用批处理)

::get javaw.exe from the latest properly installed jre
for /f tokens^=2^ delims^=^" %%i in ('reg query HKEY_CLASSES_ROOT\jarfile\shell\open\command /ve') do set JAVAW_PATH=%%i

::if reg entry is not found, java is not installed
if "%JAVAW_PATH%"=="" goto YOUR_ERROR

::then strip "\javaw.exe" from the JAVAW_PATH obtained above
set JAVA_HOME=%JAVAW_PATH:\javaw.exe=%

This should work with jre 1.6 and 1.7 installed in windows xp and seven and be way more faster than searching the filesystem.

这应该适用于安装在 windows xp 和 7 中的 jre 1.6 和 1.7,并且比搜索文件系统要快得多。