Finding 32-bit or 64-bit Java JDK Version in Java

I encountered an issue with Java installation not being compatible with my 32-bit machine. This blog post provides guidance on checking the JVM version and installing the latest Java version.

Determining 32-bit or 64-bit for Java Installation

A 32-bit system utilizes 4 bytes of memory, while a 64-bit system uses 8 bytes. This implies that 64-bit systems require more memory than their 32-bit counterparts.

Most of the time, if your code compiles with the 32-bit JDK version, you need to execute it on a 32-bit machine or 64-bit machine. This usually works fine, but machine-specific code can lead to issues, so caution is advised when installing JDK versions.

It’s important to note that a 32-bit JVM is not compatible with other 64-bit hosts/frameworks. I encountered issues with installing Tomcat 64-bit on Java 32-bit.

Java code to check 32 bit JVM or 64 Bit JVM

There is no public API to determine the 32-bit or 64-bit version according to Sun’s specifications. However, one way is to use the sun.arch.data.model system property in the JVM, which indicates the bit version. Below is a sample code:

One way is as sun.arch.data.model is one of the system properties in JVM which has 32 bit or 64 bit or unknown, so we have to write a simple program to read the property value.

Here is a sample code to find out bit version

public class BitVersionDemo {
 public static void main(String args[]) {
  System.out.println("version ="
    + System.getProperty("sun.arch.data.model"));
 }
}


Output

version =32

This output indicates that my machine has the JDK version installed with 32-bit.

on Linux:

If your system has Java 64-bit installed, running the command java -d32 will result in the message “Running a 32-bit JVM is not supported on this platform.”

bash-3.2$ java -d32 -version
Running a 32-bit JVM is not supported on this platform.

On the other hand, using the command java -d64 will display information for a 64-bit JVM.

bash-3.2$ java -d64 -version
java version "1.12.0_1"
Java(TM) SE Runtime Environment (build 1.6.0_27-b07)
Java HotSpot(TM) 64-Bit Server VM (build 20.2-b06, mixed mode)

The command “java -d32 -version” may result in an error, so the sample code above can be used to determine the version.

If you want to know the reference size, you can use Unsafe.addressSize(). Note that this is usually 4, as most 64-bit JVMs use 32-bit references for up to 32 GB of heap.

Wrap up

In conclusion, we’ve learned how to identify the installed JDK version as 64-bit or 32-bit using a Java program and command line.