views:

2584

answers:

3

How can I tell if the JVM my application runs in is 32 bit or 64-bit? Specifically, what function or preference do I access to detect this within the program?

+11  A: 

Sun has a Java System property to determine the bitness of the JVM: 32 or 64:

sun.arch.data.model=32 // 32 bit JVM
sun.arch.data.model=64 // 64 bit JVM

You can use

System.getProperty("sun.arch.data.model") 

to determine if its 32/64 from the program.

From the sun.docs:

When writing Java code, how do I distinguish between 32 and 64-bit operation?

There's no public API that allows you to distinguish between 32 and 64-bit operation. Think of 64-bit as just another platform in the write once, run anywhere tradition. However, if you'd like to write code which is platform specific (shame on you), the system property sun.arch.data.model has the value "32", "64", or "unknown".

codaddict
Is this defined by the Java spec to work under all JVMs, or is it Hotspot-specific?
BobMcGee
I wouldn't expect to find `sun.*` system properties with an IBM JVM. In other words, it's not portable.
Pascal Thivent
Is this still valid now that Java is owned by Oracle? I am running an environment-auditor that will run pre-deploy of our application. We need to verify that the 64-bit production boxes had 64-bit Java installed on them since we don't manage these machines. I need to make sure this still works and will continue to work moving forward with Oracle.
Gweebz
+3  A: 

Update Again:

I installed 32-bit JVM and retried it again, looks like the following does tell you JVM bitness, not OS arch:

System.getProperty("os.arch");
#
# on a 64-bit Linux box:
# "x86" when using 32-bit JVM
# "xmd64" when using 64-bit JVM

This was tested against both SUN and IBM JVM (32 and 64-bit). Clearly, the system property is not just the operating system arch.

bryantsai
This gives Operating system architecture info. If I'm not wrong, this need not be same of JVM bitness.
codaddict
Yeah. It gives x86, etc. Not quite the same thing, since I'm not sure if it gives "x64" for x86-64 running in 64-bit rather than 32-bit.
BobMcGee
http://forums.java.net/jive/thread.jspa?messageID=274873
Pascal Thivent
@codaddict, looks like it is JVM bitness indeed.
bryantsai
+2  A: 

You can try on the command line:

java -d64 -version

If it's not a 64-bit version, you'll get a message that looks like:

This Java instance does not support a 64-bit JVM. Please install the desired version.

gpampara
Although this is good to know, it's not useful b/c I need to run it from outside the program or use java options to start a new process.
BobMcGee
Sorry I, completely misread the question. Within Java itself, the correct manner would be as described by codadict
gpampara