14

I want to know the OS type, means is it of 64bit os or 32bit os. But I am getting different response from 32bit/64bit executable which getting system properties about OS

I am getting details from system property. and I have 64bit machine and OS windows 8.1 X64

        "OS Architecture : " + System.getProperty("os.arch"));

        "OS Name : " + System.getProperty("os.name"));

        "OS Version : " + System.getProperty("os.version")

        "Data Model : " + System.getProperty("sun.arch.data.model"));


While running 64bit Executable getting following response.


OS Architecture : amd64

OS Name : Windows 8

OS Version : 6.2 amd64

Data Model : 64


While running 32bit Executable getting following response.


OS Architecture : x86

OS Name : Windows 8

OS Version : 6.2 x86

Data Model : 32


How can I get OS actual bit type ?

Tej Kiran
  • 2,218
  • 5
  • 21
  • 42

3 Answers3

2

The best solution (which is also cross-platform IMO) is the answer given here: https://stackoverflow.com/a/2269242/1973164

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.

Community
  • 1
  • 1
BitParser
  • 3,748
  • 26
  • 42
  • 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:20
  • @KarlRichter Whilst the _answer_ you have linked to sounds reasonable, you are using a subjective _answer_ as a guidance to downvote other answers. I'm not linking to content outside of SO, rather, I'm linking to content already here. Meanwhile, I suggest you take a look at [the tour](http://stackoverflow.com/tour), which specifically mentions to use comments in case you need clarification, and _edit_ the answer if you believe you have a _fix_; I believe you might have missed it. – BitParser Dec 23 '16 at 10:04
2

System.getProperty("os.arch") will always return the bit type of the jre and not of the actual operating system.

Write jni code and calll IsWow64Process from winapi. Since you are using windows.

This boolean function tells you whether the process is running on 64-bit os.

Sorter
  • 9,704
  • 6
  • 64
  • 74
0

This should help you

System.getProperty("os.arch");

will return "x86" when the architecture of JRE is 32 bit. We have to write native code to get the real architecture of the system using JNI.

Keerthivasan
  • 12,760
  • 2
  • 32
  • 53
  • 1
    This property is giving unexpected result - while running 32bit executable giving x86 and for same code if run for 64bit executable then return amd64 even operating system is 64-bit – Tej Kiran Dec 31 '13 at 11:36
  • if the value contains x86, it should be a 32 bit os otherwise, it will be 64-bit. Am i right? Why is it downvoted? – Keerthivasan Dec 31 '13 at 11:41
  • 1
    os.arch returns the JVM characteristics - a 32 bit jvm running on a 64 bit os will return x86 for os.arch. – assylias Dec 31 '13 at 11:44
  • @assylias yes, i got it. I have updated my answer as well. – Keerthivasan Dec 31 '13 at 11:45