简体   繁体   English

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

[英]How can I check the bitness of my OS using Java? (J2SE, not os.arch)

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.我正在开发一个软件应用程序来检查您安装了什么样的软件,但为了做到这一点,我必须知道操作系统是 32 位还是 64 位操作系统。

I tried System.getProperty("os.arch");我试过 System.getProperty("os.arch"); but then I read that this command only shows us the bitness of the JDK/JRE, not the OS itself.但后来我读到这个命令只向我们展示了 JDK/JRE 的位数,而不是操作系统本身。 If you could tell me how to know which OS is being used (Windows 7, Mac OS, Ubuntu, etc...) that would be simply awesome.如果您能告诉我如何知道正在使用哪个操作系统(Windows 7、Mac OS、Ubuntu 等),那就太棒了。

System.getProperty("os.arch");

Should be available on all platforms, see the Java System Properties Tutorial for more information.应该在所有平台上都可用,有关更多信息,请参阅 Java 系统属性教程

But 64 bit Windows platforms will lie to the JVM if it is a 32 bit JVM.但是,如果64 位 Windows 平台是 32 位 JVM,那么 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.实际上,64 位 Windows 会欺骗任何有关环境的 32 位进程,以帮助旧的 32 位程序在 64 位操作系统上正常工作。 Read the MSDN article about WOW64 for more information.阅读有关 WOW64MSDN 文章以获取更多信息。

As a result of WOW64, a 32 bit JVM calling System.getProperty("os.arch") will return "x86".作为 WOW64 的结果,调用System.getProperty("os.arch")的 32 位 JVM 将返回“x86”。 If you want to get the real architecture of the underlying OS on Windows, use the following logic:如果你想在 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";

See also:也可以看看:

HOWTO: Detect Process Bitness 如何:检测进程位数

Why %processor_architecture% always returns x86 instead of AMD64 为什么 %processor_architecture% 总是返回 x86 而不是 AMD64

Detect whether current Windows version is 32 bit or 64 bit 检测当前 Windows 版本是 32 位还是 64 位

There is no way to do this without getting plattform specific.如果没有获得特定于平台的信息,就无法做到这一点。 Have a look at the last post on this page (the solution there is plattform specific).看看这个页面上的最后一篇文章(那里的解决方案是特定于平台的)。

The property os.name gives you the name of the used operating system, os.version the version.属性os.name为您提供所用操作系统的名称, os.version版本。

You can check by calling您可以拨打电话查询

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

This line returns 32 or 64 which identifies if the JVM is either 32 or 64 bits.此行返回 32 或 64,用于标识 JVM 是 32 位还是 64 位。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM