Find out 32 bit or 64-bit java JDK version in Java

I have encountered one of the issues with java installation that is not compatible with my 32-bit machine.

This blog post is helpful for to check the JVM version to install the latest java version install.

Check 32 bit or 64 bit for Java installation

32 bit is 4 bytes of size, whereas 64 bit is 8 bytes of size. That means 64-bit takes more memory than 32 machines.

Most of the time, if your code compiles with the 32-bit JDK version, then you need to execute these class files in a 32-bit machine/64-bit machine only. This will compile and execute fine, but in some cases, if there is machine-specific code, you are in trouble with issues, so careful while installing JDK versions.

32-bit JVM is not compatible with other 64-bit hosts/frameworks as I have encountered issues with the installation of tomcat 64 bit on java 32 bit.

Java program to check 32 bit JVM or 64 Bit JVM

I am listing down the ways to find out the 32 bit vs 64 bit.

As per Sun Specification There is no public API to find out the 32-bit or 64 bit.

There are many ways to find out whether the 32bit version or not.

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

that means My machine installed the JDK version with 32-bit.

on Linux:

so if your system is installed java 64 bit, by running the java -d32 command it gives the following message.

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

By giving the -d64 command, the following information is displayed.

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)

On windows, “java -d32 -version” is giving an error so you can write the above sample to find out the version

If you want to know what the reference size is you can use Unsafe.addressSize(). Note: this is usually 4 as most 64-bit JVM will be using 32-bit references for up to 32 GB of heap.

Wrap up

To sum up, We learned how to find the installed JDK version on 64-bit or 32-bit using the java program and command line.