Java 如何查找操作系统位类型

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20856694/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 04:50:03  来源:igfitidea点击:

How to find the OS bit type

javaarchitecturepropertiesoperating-system

提问by Tej Kiran

I want to know the OS type, means is it of 64bit os or 32bit os. But I am getting different response from 32bit/64bit executable which getting system properties about OS

我想知道操作系统类型,是指 64 位操作系统还是 32 位操作系统。但是我从 32 位/64 位可执行文件得到不同的响应,它获取有关操作系统的系统属性

I am getting details from system property. and I have 64bit machine and OS windows 8.1 X64

我正在从系统属性中获取详细信息。我有 64 位机器和操作系统 windows 8.1 X64

        "OS Architecture : " + System.getProperty("os.arch"));

        "OS Name : " + System.getProperty("os.name"));

        "OS Version : " + System.getProperty("os.version")

        "Data Model : " + System.getProperty("sun.arch.data.model"));



While running 64bit Executablegetting following response.

在运行64 位可执行文件时获得以下响应。



OS Architecture: amd64

操作系统架构amd64

OS Name: Windows 8

操作系统名称:Windows 8

OS Version: 6.2 amd64

操作系统版本:6.2 amd64

Data Model: 64

数据模型:64



While running 32bit Executablegetting following response.

在运行32 位可执行文件时获得以下响应。



OS Architecture: x86

操作系统架构x86

OS Name: Windows 8

操作系统名称:Windows 8

OS Version: 6.2 x86

操作系统版本:6.2 x86

Data Model: 32

数据模型:32



How can I get OS actual bit type ?

如何获得操作系统实际位类型?

回答by Keerthivasan

This should help you

这应该可以帮助你

System.getProperty("os.arch");

will return "x86" when the architecture of JRE is 32 bit. We have to write native code to get the real architecture of the system using JNI.

当 JRE 的架构为 32 位时,将返回“x86”。我们必须编写本机代码才能使用 JNI 获得系统的真实架构。

回答by TGO

The best solution (which is also cross-platform IMO) is the answer given here: https://stackoverflow.com/a/2269242/1973164

最佳解决方案(也是跨平台 IMO)是此处给出的答案:https: //stackoverflow.com/a/2269242/1973164

I don't exactly trust reading the os.arch system variable. While it works if a user is running a 64bit JVM on a 64bit system. It doesn't work if the user is running a 32bit JVM on a 64 bit system.

The following code works for properly detecting Windows 64-bit operating systems. On a Windows 64 bit system the environment variable "Programfiles(x86)" will be set. It will NOT be set on a 32-bit system and java will read it as null.

boolean is64bit = false;
if (System.getProperty("os.name").contains("Windows")) {
    is64bit = (System.getenv("ProgramFiles(x86)") != null);
} else {
    is64bit = (System.getProperty("os.arch").indexOf("64") != -1);
}

For other operating systems like Linux or Solaris or Mac we may see this problem as well. So this isn't a complete solution. For mac you are probably safe because apple locks down the JVM to match the OS. But Linux and Solaris, etc.. they may still use a 32-bit JVM on their 64-bit system. So use this with caution.

我并不完全相信阅读 os.arch 系统变量。如果用户在 64 位系统上运行 64 位 JVM,它可以工作。如果用户在 64 位系统上运行 32 位 JVM,则它不起作用。

以下代码适用于正确检测 Windows 64 位操作系统。在 Windows 64 位系统上,将设置环境变量“Programfiles(x86)”。它不会在 32 位系统上设置,java 会将其读取为 null。

boolean is64bit = false;
if (System.getProperty("os.name").contains("Windows")) {
    is64bit = (System.getenv("ProgramFiles(x86)") != null);
} else {
    is64bit = (System.getProperty("os.arch").indexOf("64") != -1);
}

对于 Linux 或 Solaris 或 Mac 等其他操作系统,我们也可能会遇到此问题。所以这不是一个完整的解决方案。对于 mac 你可能是安全的,因为苹果锁定了 JVM 以匹配操作系统。但是 Linux 和 Solaris 等。他们可能仍然在他们的 64 位系统上使用 32 位 JVM。所以请谨慎使用。

回答by Sorter

System.getProperty("os.arch") will always return the bit type of the jre and not of the actual operating system.

System.getProperty("os.arch") 将始终返回 jre 的位类型,而不是实际操作系统的位类型。

Write jni code and calll IsWow64Processfrom winapi. Since you are using windows.

编写 jni 代码并IsWow64Process从 winapi调用。由于您使用的是windows。

This boolean function tells you whether the process is running on 64-bit os.

这个布尔函数告诉你进程是否在 64 位操作系统上运行。