Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

operating system - 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.

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.

Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
System.getProperty("os.arch");

Should be available on all platforms, see the Java System Properties Tutorial for more information.

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.

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";

See also:

HOWTO: Detect Process Bitness

Why %processor_architecture% always returns x86 instead of AMD64

Detect whether current Windows version is 32 bit or 64 bit


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...