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

2023-02-21Java开发问题
16

本文介绍了如何使用 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)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

如何使用 JAVA 向 COM PORT 发送数据?
How to send data to COM PORT using JAVA?(如何使用 JAVA 向 COM PORT 发送数据?)...
2024-08-25 Java开发问题
21

如何使报表页面方向更改为“rtl"?
How to make a report page direction to change to quot;rtlquot;?(如何使报表页面方向更改为“rtl?)...
2024-08-25 Java开发问题
19

在 Eclipse 项目中使用西里尔文 .properties 文件
Use cyrillic .properties file in eclipse project(在 Eclipse 项目中使用西里尔文 .properties 文件)...
2024-08-25 Java开发问题
18

有没有办法在 Java 中检测 RTL 语言?
Is there any way to detect an RTL language in Java?(有没有办法在 Java 中检测 RTL 语言?)...
2024-08-25 Java开发问题
11

如何在 Java 中从 DB 加载资源包消息?
How to load resource bundle messages from DB in Java?(如何在 Java 中从 DB 加载资源包消息?)...
2024-08-25 Java开发问题
13

如何更改 Java 中的默认语言环境设置以使其保持一致?
How do I change the default locale settings in Java to make them consistent?(如何更改 Java 中的默认语言环境设置以使其保持一致?)...
2024-08-25 Java开发问题
13