1

I want to determine the bit architecture of my OS i.e whether it is a 32-bit OS or a 64-bit OS using Java code.

I have tried the following:

  System.out.printlnSystem.getProperty("os.name"));     //gives OS name -> Windows/Linux/Solaris
  System.out.println(System.getProperty("os.arch"));   //gives JVM architecture -> amd64
  System.out.println(System.getProperty("sun.arch.data.model"));   //gives JVM architecture again but just the bit -> 64

However none of the above give me the OS architecture. They give the respective output which is mentioned in comments besides the code lines.

How can I determine the OS architecture ?

Josh Harrison
  • 5,927
  • 1
  • 30
  • 44
Crusaderpyro
  • 2,163
  • 5
  • 29
  • 53
  • `os.arch` tells you the JRE arch your program is running on. Unfortunately there is no direct way to know the os architecture, you'll have to go on with JNI. – BackSlash Jul 11 '14 at 10:02
  • What's wrong with "os.arch"? It already returns amd64? – isnot2bad Jul 11 '14 at 10:05
  • @BackSlash - Yes. It gives the JVM architecture. So to find the OS architecture do I have to scan through the registry ? seems like the only option remaining. – Crusaderpyro Jul 11 '14 at 10:05
  • 1
    @Crusaderpyro I guess yes. Or maybe you can execute a command which returns the os arch (on linux `uname -m`). But, as JNI, this is OS dependent, so you'll have to get the os name first and then switch to select the proper command to run – BackSlash Jul 11 '14 at 10:09
  • This is not a duplicate. The linked question is for Windows only and the answers are Windows-specific. The question how to do this cross platform (the much better question imho) remains unanswered. – bluenote10 Aug 23 '14 at 09:34

2 Answers2

0

You can check the environment variable for example for Windows

System.out.println(System.getenv().get("PROCESSOR_ARCHITECTURE"));
Roman C
  • 49,761
  • 33
  • 66
  • 176
0

I hope this one line might help you.

public class OS {

public static void main(String[] args) {
    //System.out.println("OS name : "+System.getProperty("os.name"));
    //System.out.println("OS arch : "+System.getProperty("os.arch"));
    //System.out.println(System.getProperty("sun.arch.data.model")); 
    //System.out.println(System.getenv().get("PROCESSOR_ARCHITECTURE"));


    System.getProperties().list(System.out);

 }
}
royki
  • 1,593
  • 3
  • 25
  • 45