Beware when detecting 32 and 64 bit OS’s in Java

These days quite some systems are 64-bit systems, however Java still ships a mix of 32 and 64 bit JVMs.

Confusion can happen if you run a 32-bit JVM on a 64-bit OS.

Java in general will run fine but if your code tries to detect what kind of OS you are actually running, you might run into trouble.

Java guides typically state to use the system property “os.arch” to get to the type of os architecture you are running. But is that correct ?

As a test I wrote the simplist program :

[code language=”css”]
public class TellMeOS {

public static void main(String[] args) {
System.out.println("OS name : "+System.getProperty("os.name"));
System.out.println("OS arch : "+System.getProperty("os.arch"));

}
}
[/code]

So let us try this out with a 64-bit JVM :

C:\>set PATH=C:\oracle\jrockit-jdk1.6.0_20-R28.1.0-4.0.1\bin
C:\>java -version
java version "1.6.0_20"
Java(TM) SE Runtime Environment (build 1.6.0_20-b02)
Oracle JRockit(R) (build R28.1.0-123-138454-1.6.0_20-20101014-1351-windows-x86_64, compiled mode)
C:\>java -jar "C:\workspaces\TellMeOS\dist\TellMeOS.jar"
OS name : Windows 7
OS arch : amd64

This give me the correct answer ( I am running Win7-64 bit on a 64-bit Dell laptop )

Now let us try with a 32-bit JVM :

C:\>set PATH=C:\oracle\wls1036\jrockit_160_29_D1.2.0-10\bin
C:\>java -version
java version "1.6.0_29"
Java(TM) SE Runtime Environment (build 1.6.0_29-b11)
Oracle JRockit(R) (build R28.2.0-79-146777-1.6.0_29-20111005-1808-windows-ia32, compiled mode)
C:\>java -jar "C:\workspaces\TellMeOS\dist\TellMeOS.jar"
OS name : Windows 7
OS arch : x86

So, this is giving me a result stating I am running on a 32-bit x86 architecture. Of course my hardware did not change in between tests, only the JVM did.

Conclusion : “os.arch” does not give you the right OS architecture but only the JVM architecture.

Post navigation

Geef een reactie

Het e-mailadres wordt niet gepubliceerd. Verplichte velden zijn gemarkeerd met *