如何使用 Java 检查操作系统的位数?(J2SE,不是 os.arch) [英] How can I check the bitness of my OS using Java? (J2SE, not os.arch)

查看:33
本文介绍了如何使用 Java 检查操作系统的位数?(J2SE,不是 os.arch)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个软件应用程序来检查您安装了哪种软件,但为了这样做,我必须知道操作系统是 32 位还是 64 位操作系统.

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.

我试过 System.getProperty("os.arch");但后来我读到这个命令只向我们展示了 JDK/JRE 的位数,而不是操作系统本身.如果您能告诉我如何知道正在使用的操作系统(Windows 7、Mac OS、Ubuntu 等),那就太棒了.

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 which OS is being used (Windows 7, Mac OS, Ubuntu, etc...) that would be simply awesome.

推荐答案

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 的结果,调用 System.getProperty("os.arch") 的 32 位 JVM 将返回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

检测当前Windows版本是否为32位或 64 位

这篇关于如何使用 Java 检查操作系统的位数?(J2SE,不是 os.arch)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆