27

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.

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
Raúl Núñez Cuevas
  • 838
  • 4
  • 10
  • 14
  • Well yeah, that's what I thought, but then I saw this link, you might want to check it out: http://mark.koli.ch/2009/10/javas-osarch-system-property-is-the-bitness-of-the-jre-not-the-operating-system.html tell me what you think – Raúl Núñez Cuevas Jan 20 '11 at 15:16

4 Answers4

50
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

Community
  • 1
  • 1
ChrisH
  • 4,788
  • 26
  • 35
  • I get NPE's on the realArch line on mac osx :( – user2693017 Jun 17 '14 at 13:24
  • Works better than our previous implementation. Thanks! – CMerrill Jul 28 '14 at 15:08
  • I want to apologize, all this time I though I had your answer selected as the correct answer! But it's done now (: – Raúl Núñez Cuevas Aug 28 '14 at 16:35
  • How about MacOS and Linux? –  Mar 29 '18 at 06:58
  • 1
    @user7485... I think os.arch is always the bitness of the JRE, not the OS. e.g. [System.getProperty("os.arch") in java code yields i386 os architecture, but my ubuntu is actually x86\_64. Why?](https://stackoverflow.com/q/60730849). It's not Windows that lies to Java in this case, it's that `os.arch` means the bitness of the JRE. – Peter Cordes Mar 17 '20 at 22:10
  • 1
    For MacOS, if arch & wow64arch are null, use arch = System.getProperty( "os.arch" ); – Waverick Jun 13 '20 at 08:47
  • 1
    in my case the command prompt showing the following value `C:\Users\Administrator\Desktop>echo %PROCESSOR_ARCHITECTURE% AMD64` but `String procArch = System.getenv("PROCESSOR_ARCHITECTURE"); System.out.println(procArch);` is showing still x86 only. Any Idea? – Nagaraju Chitimilla Mar 23 '21 at 10:36
2

os.arch is NOT the bitness of the OS, beware of this solution! http://mark.koli.ch/2009/10/javas-osarch-system-property-is-the-bitness-of-the-jre-not-the-operating-system.html

schlingel
  • 8,560
  • 7
  • 34
  • 62
  • 2
    Whilst this may theoretically answer the question, [it would be preferable](//meta.stackoverflow.com/q/8259) to include the essential parts of the answer here, and provide the link for reference. – Kalle Richter Dec 22 '16 at 19:22
1

There is no way to do this without getting plattform specific. Have a look at the last post on this page (the solution there is plattform specific).

The property os.name gives you the name of the used operating system, os.version the version.

Matten
  • 17,365
  • 2
  • 42
  • 64
-2

You can check by calling

System.getProperty("sun.arch.data.model");

This line returns 32 or 64 which identifies if the JVM is either 32 or 64 bits.

Rodrigo Camargo
  • 180
  • 1
  • 6