windows 以编程方式确定我的机器上安装了哪些 JDK/JRE
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1189192/
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
Programmatically determine what JDK/JRE's are installed on my box
提问by Amir Afghani
Is there a standard way to do this? I realize this can be somewhat platform dependent. Our product right now is only supported on Windows - so I suppose that's what I'm interested in right now. The only things I can think of are to either scan the registry or crawl the file system. Scanning the file system seems like it can take a really long time - and the registry can be unreliable. Should I do both? Any other suggestions? I tried to look for an API to do this with no luck.
有没有标准的方法来做到这一点?我意识到这可能有点依赖平台。我们的产品目前仅在 Windows 上受支持 - 所以我想这就是我现在感兴趣的。我唯一能想到的就是扫描注册表或抓取文件系统。扫描文件系统似乎需要很长时间——而且注册表可能不可靠。我应该两者都做吗?还有其他建议吗?我试图寻找一个 API 来做到这一点,但没有运气。
采纳答案by cletus
I would firstly start by looking for the JAVA_HOME environment variable (and possibly JDK_HOME although thats far less common) and then determining what version that is and whether it's a JDK or JRE.
我首先会查找 JAVA_HOME 环境变量(也可能是 JDK_HOME,虽然这很不常见),然后确定它是什么版本以及它是 JDK 还是 JRE。
After that check for common instead locations. Find out the system's program files directory (don't just assume it's C:\Program Files even though it is 99.5% of the time) and look for common install locations under that (eg Java).
之后检查常见的位置。找出系统的程序文件目录(不要只是假设它是 C:\Program Files,即使它是 99.5% 的时间)并在该目录下寻找常见的安装位置(例如 Java)。
I wouldn't do an exhaustive search.
我不会进行详尽的搜索。
It's worth asking: do you really need to find JDKs this way? Can't you just ask the user what JDK he or she wishes to use, possibly suggesting any easy ones you've found already?
值得一问:你真的需要这样寻找JDK吗?您不能只询问用户他或她希望使用什么 JDK,可能建议您已经找到的任何简单的 JDK?
回答by Denis Tulskiy
回答by Mike McQuaid
I'd probably go for a combination of looking for the Java installed registry keys and crawling the defaultlocations for installation (which shouldn't take too long).
我可能会结合查找 Java 安装的注册表项和抓取安装的默认位置(这应该不会花费太长时间)。
An alternative approach would be to bundle a tiny Java application which prints various details such as the running JVM.
另一种方法是捆绑一个微型 Java 应用程序,该应用程序打印各种详细信息,例如正在运行的 JVM。