Java Program to Check if JVM is 32 or 64 bit


Before making java program to check if JVM is 32 or 64 bit, let’s first discuss about JVM.

JVM is java virtual machine that is responsible for executing the byte code. It is a part of Java Runtime Environment (JRE). We all know java is platform independent but the JVM is platform dependent. We need separate JVM for every OS. If we have byte code of any java source code, we can run it on any platform easily because of JVM. The whole process of execution of a java file is as follows −

  • First, we save java source code with extension .java that is converted by compiler to byte code with extension .class. This happens at the compile time.

  • Now at the runtime, JVM reads and verifies byte code, allocates memories to variables and then, converts that byte code to machine readable form.

We can check if JVM is 32 or 64 bit by using two system properties ‘os.arch’ and ‘sun.arch.data.model’ in java program. There is another approach also, we can check it through command line by using ‘java --version’.

getProperty()

In this article, we will use this method to fetch the details of JVM. It is present in System class. It helps in retrieving system property of specified argument. If the parameter exists it returns a string that contains the value of the given parameter otherwise it returns null.

Syntax

System.getProperty(key); 

Here, ‘key’ accepts argument.

Approach 1: Using os.arch

Example

public class Main {
   public static void main(String[] args) {
      String info = System.getProperty("os.arch");
      System.out.println(info + "-bit JVM is installed in your device ");
   }
}

Output

amd64-bit JVM is installed in your device

In the above code, we have declared a string variable named ‘info’ to store the value returned by ‘getProperty()’ method.

Approach 2: Using sun.arch.data.model

Example

public class Main {
   public static void main(String[] args) {
      String info = System.getProperty("sun.arch.data.model");
      if (info.equals("64")) {
         System.out.println(info + "-bit JVM is installed in your device ");
      } else {
         System.out.println(info + "-bit JVM is installed in your device ");
      }
   }
}

Output

64-bit JVM is installed in your device

In the above code, we have declared a string variable named ‘info’ to store the value returned by ‘getProperty()’ method. In the if else block, we have checked if returned string is equal to ‘64’ or not using ‘equals()’ method. The ‘equals()’ method has been used to compare two strings. Its return type is boolean, if both strings are equal it returns true otherwise false. In this case, if block is true. Hence, it has printed 64 bit.

Approach 3: Using Command Line Interface

Open cmd in your device and type ‘java --version’. When you press enter, you will see the result depending on the configuration of your device −

C:\Users\Lenovo>java --version
java 17.0.6 2023-01-17 LTS
Java(TM) SE Runtime Environment (build 17.0.6+9-LTS-190)
Java HotSpot(TM) 64-Bit Server VM (build 17.0.6+9-LTS-190, mixed mode, sharing)

Conclusion

JVM is platform dependent machine that converts byte code to machine readable format. The main difference between the 32 bit and 64 bit JVM is the memory size limit. We can specify a maximum of 4 GB in case of 32 bit but 64 bit has much more capacity. In this article, we have discussed two java programs to check if JVM is 32 or 64 bit.

Updated on: 25-Apr-2023

615 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements