問題描述
我正在開發一個軟件應用程序來檢查您安裝了哪種軟件,但為了這樣做,我必須知道操作系統是 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)的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!