Java 如何在 Windows 上检测已安装的 Sun/Oracle JRE?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1339910/
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
How can I detect the installed Sun/Oracle JRE on Windows?
提问by Daniel Rikowski
I tried googling the answer, but all I found was tips on how to detect Java from a browser or the very generic way of just starting Java and see if it runs, which introduces a possibly long delay in my application. (~ two seconds when started the very first time on my machine)
我尝试在谷歌上搜索答案,但我发现的只是关于如何从浏览器检测 Java 的提示,或者只是启动 Java 并查看它是否运行的非常通用的方法,这可能会在我的应用程序中引入很长时间的延迟。(~第一次在我的机器上启动时两秒钟)
I hope there is a faster way, if the following restrictions apply:
如果以下限制适用,我希望有更快的方法:
- Only Sun/Oracle JREs or JDKs
- Only 1.6 and higher
- Only Windows platforms
- Notfrom a browser, but from a plain old Win32 executable
- 仅 Sun/Oracle JRE 或 JDK
- 仅 1.6 及更高版本
- 仅限 Windows 平台
- 不是来自浏览器,而是来自一个普通的旧 Win32 可执行文件
This detection is not meant for a public application, but for internal use on Windows platforms only.
此检测不适用于公共应用程序,仅适用于 Windows 平台上的内部使用。
Is there a registry path I can read or some configuration file I can parse?
是否有我可以读取的注册表路径或一些我可以解析的配置文件?
采纳答案by finnw
HKEY_LOCAL_MACHINE\SOFTWARE\JavaSoft\Java Runtime Environment
回答by McDowell
The registry will probably be the easiest route - assuming that an installer has been run. Installed versions can be found in various subkeys under:
注册表可能是最简单的途径——假设安装程序已经运行。可以在以下各种子项中找到已安装的版本:
HKEY_LOCAL_MACHINE\SOFTWARE\JavaSoft\Java Development Kit
HKEY_LOCAL_MACHINE\SOFTWARE\JavaSoft\Java Runtime Environment
If the user has manually configured their environment, you could check JAVA_HOME
/walk the PATH
variable and check the file version. Demo WSH script:
如果用户手动配置了他们的环境,您可以检查JAVA_HOME
/walkPATH
变量并检查文件版本。演示 WSH 脚本:
'file: whereJava.vbs
'usage: cscript /Nologo whereJava.vbs
'find Java 6 from registry
Set objShell = CreateObject("WScript.Shell")
Wscript.Echo objShell.RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\" &_
"JavaSoft\Java Runtime Environment.6\JavaHome")
'check file version of java.exe
javaHome = objShell.Environment.item("JAVA_HOME")
Set objFSO = CreateObject("Scripting.FileSystemObject")
Wscript.Echo objFSO.GetFileVersion(javaHome & "\bin\java.exe")
See GetFileVersionInfoand company. The major version numbers seem to match the Java version (5, 6). There's a finite amount you can do without invoking the JVM.
请参阅GetFileVersionInfo和公司。主要版本号似乎与 Java 版本 (5, 6) 匹配。您可以在不调用 JVM 的情况下执行有限的操作。
回答by McDowell
Instead you can try running the command "java -version" in command prompt.
相反,您可以尝试在命令提示符下运行命令“java -version”。
This may not actually work well if the JRE is not properly installed but copied from some other machine. A Sure shot workaround is to navigate to the JRE installation directory "C:\Program Files\Java\", navigate to the bin folder from command prompt and then run "java -version". Output will be a installation version, and all relevant information you are looking for.
如果 JRE 没有正确安装而是从其他机器复制,这实际上可能无法正常工作。一个肯定的解决方法是导航到 JRE 安装目录“ C:\Program Files\Java\”,从命令提示符导航到 bin 文件夹,然后运行“ java -version”。输出将是一个安装版本,以及您正在寻找的所有相关信息。
回答by mfx
There can be any number of installaed JREs and JDKs on a windows machine, but only one will have the HKEY_LOCAL_MACHINE\SOFTWARE\JavaSoft\Java Runtime Environment set.
Windows 机器上可以安装任意数量的 JRE 和 JDK,但只有一个会设置 HKEY_LOCAL_MACHINE\SOFTWARE\JavaSoft\Java 运行时环境。
You might also consider the "JAVA_HOME" and "Path" environment variables, as they will influence command-line java invocations.
您还可以考虑“JAVA_HOME”和“Path”环境变量,因为它们会影响命令行 java 调用。
回答by grokster
Windows > Start > cmd >
Windows > 开始 > cmd >
C:> for %i in (java.exe) do @echo. %~$PATH:i
If you have a JRE installed, the Path is displayed,
for example: C:\Windows\System32\java.exe
如果您安装了 JRE,则会显示路径,例如: C:\Windows\System32\java.exe