如何使用Java检查操作系统的位数? (J2SE,不是os.arch) [英] How can I check the bitness of my OS using Java?? (J2SE, not os.arch)
问题描述
我正在开发一个软件应用程序,用于检查您安装的软件类型,但为了做到这一点,我必须知道操作系统是32位还是64位操作系统。我试过System.getProperty(os.arch);但后来我读到这个命令只向我们展示了JDK / JRE的位,而不是操作系统本身。如果你能告诉我如何知道操作系统正在被使用(Windows 7,Mac OS,Ubuntu等等),那将是非常棒的C:
I'm developing a software application that checks what kind of software you have installed, but in order to do so, I must know if the OS is a 32 bit or a 64 bit OS. I tried System.getProperty("os.arch"); but then I read that this command only shows us the bitness of the JDK/JRE, not the OS itself. If you could tell me how to know wich OS is being used (Windows 7, Mac OS, Ubuntu, etc...) that would be simply awesome C:
推荐答案
System.getProperty("os.arch");
应该可以在所有平台上使用,请参阅Java 系统属性教程了解更多信息。
Should be available on all platforms, see the Java System Properties Tutorial for more information.
但是如果它是32位JVM,64位Windows平台将存在于JVM中。实际上64位Windows将涉及任何关于环境的32位进程,以帮助旧的32位程序在64位操作系统上正常工作。阅读有关WOW64的MSDN文章有关更多信息。
But 64 bit Windows platforms will lie to the JVM if it is a 32 bit JVM. Actually 64 bit Windows will lie to any 32 bit process about the environment to help old 32 bit programs work properly on a 64 bit OS. Read the MSDN article about WOW64 for more information.
作为WOW64的结果,32位JVM调用 System.getProperty(os.arch)
将返回x86。如果您想在Windows上获得底层操作系统的真实架构,请使用以下逻辑:
As a result of WOW64, a 32 bit JVM calling System.getProperty("os.arch")
will return "x86". If you want to get the real architecture of the underlying OS on Windows, use the following logic:
String arch = System.getenv("PROCESSOR_ARCHITECTURE");
String wow64Arch = System.getenv("PROCESSOR_ARCHITEW6432");
String realArch = arch != null && arch.endsWith("64")
|| wow64Arch != null && wow64Arch.endsWith("64")
? "64" : "32";
参见:
为什么%processor_architecture%总是返回x86而不是AMD64
这篇关于如何使用Java检查操作系统的位数? (J2SE,不是os.arch)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!