Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 567
Matthew Avatar asked Dec 06 '09 20:12

Matthew


People also ask

How do I know if JDK is 32 or 64-bit Windows 10?

$> java -d64 -version You can run this command on 32-bit and 64-bit JDKs, but it produces different result, where you can understand which kind of JDK you are using.

What is 32-bit and 64-bit Java?

32-bits and 64-bits JVMs use different native data type sizes and memory-address spaces. 64-bits JVMs can allocate (can use) more memory than the 32-bits ones. 64-bits use native datatypes with more capacity but occupy more space. Because that, the same Object may occupy more space too.

What is the difference between 32-bit Java and 64-bit Java?

That's all about the difference between 32-bit and 64-bit JVM in Java. As you have seen, the key difference comes in how much memory you can allocate, while 32-bit JVM can just have 4G which is very less for modern, memory-intensive Java application, 64-bit JVM virtually gives you unlimited memory.


2 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.

like image 125
Boolean Avatar answered Oct 17 '22 06:10

Boolean


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)

like image 24
James Van Huis Avatar answered Oct 17 '22 06:10

James Van Huis