如何使用Java检查操作系统的位数? (J2SE,而不是os.arch)

时间:2011-01-20 15:02:07

标签: java operating-system

我正在开发一个软件应用程序,用于检查您安装的软件类型,但为了做到这一点,我必须知道操作系统是32位还是64位操作系统。我试过System.getProperty(“os.arch”);但后来我读到这个命令只向我们展示了JDK / JRE的位,而不是操作系统本身。如果你能告诉我如何知道操作系统的使用(Windows 7,Mac OS,Ubuntu等等),那简直太棒了C:

4 个答案:

答案 0 :(得分:45)

System.getProperty("os.arch");

应该可以在所有平台上使用,有关详细信息,请参阅Java System Properties Tutorial

如果是32位JVM,64位Windows平台将存在于JVM中。实际上64位Windows将涉及任何关于环境的32位进程,以帮助旧的32位程序在64位操作系统上正常工作。有关详细信息,请阅读MSDN article about WOW64

作为WOW64的结果,32位JVM调用System.getProperty("os.arch")将返回“x86”。如果要在Windows上获取底层操作系统的真实体系结构,请使用以下逻辑:

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";

另见:

HOWTO: Detect Process Bitness

Why %processor_architecture% always returns x86 instead of AMD64

Detect whether current Windows version is 32 bit or 64 bit

答案 1 :(得分:2)

os.arch并不是操作系统的一点点,要小心这个解决方案! http://mark.koli.ch/2009/10/javas-osarch-system-property-is-the-bitness-of-the-jre-not-the-operating-system.html

答案 2 :(得分:1)

如果没有特定的平台,就无法做到这一点。 请查看this页面上的最后一篇文章(解决方案有特定的平台)。

属性os.name为您提供所用操作系统的名称,os.version版本。

答案 3 :(得分:-1)

您可以致电

查看
System.getProperty("sun.arch.data.model");

该行返回32或64,用于标识JVM是32位还是64位。