How do you determine 32 or 64 bit architecture of Windows using Java?

Viewed 59644

How do you determine 32 or 64 bit architecture of Windows using Java?

10 Answers

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.

Please note, the os.arch property will only give you the architecture of the JRE, not of the underlying os.

If you install a 32 bit jre on a 64 bit system, System.getProperty("os.arch") will return x86

In order to actually determine the underlying architecture, you will need to write some native code. See this post for more info (and a link to sample native code)

(Only for Windows) Check if C:\Windows\SysWOW64 exists. if the directory exist, it is a 64 bit process. Else, it is a 32 bit process.

You can get the "real" os arch from command line. You can use "wmic cpu get AddressWidth".

enter image description here

If do you want the processor arch, you can use "wmic cpu get DataWidth"

I write a simple java static code to try this.

(Disclaimer: You need use ProcessBuilder if you want reuse this code)

public static String getOsArch() {
    String arch = null;
    try {
        arch = HotUtils.executeCmd("wmic cpu get AddressWidth");
        arch = arch.trim();
        arch = arch.replace("\n\n", "\n");
        arch = arch.replace(" ", "");
        arch = arch.trim();
        return arch.endsWith("64")? "64": "32";
    } catch (IOException ex) {
        Logger.getLogger(HotUtils.class.getName()).log(Level.SEVERE, null, ex);
    }
    return arch;
}

public static void main(String[] args) {
    System.out.println(HotUtils.getOsArch());
}

public static String executeCmd(String cmd) throws IOException {
    Runtime rt = Runtime.getRuntime();
    Process proc = rt.exec(cmd);

    BufferedReader stdInput = new BufferedReader(new InputStreamReader(proc.getInputStream()));
    BufferedReader stdError = new BufferedReader(new InputStreamReader(proc.getErrorStream()));

    StringBuilder out = new StringBuilder();
    String s;
    while ((s = stdInput.readLine()) != null) {
        out.append(s);
    }

    while ((s = stdError.readLine()) != null) {
        out.append(s);
    }
    return out.toString();
}
Related