views:

30

answers:

2

I am trying to call native executables from java program. I have three exe files one for win32, other linux 32-bint and third linux 64-bit, now before I can call the right executable I need to determine which platform program is running on. I can determind Operating system by simply getting "os.name" System property, but not sure how to get determine if it is 32-bit or 64-bit platform?

A: 

I don't think that this is possible without native calls (e.g. to query the registry), but you can use the system property os.arch to determine the bitness of the JRE which is currently running the application.

(if somebody run a 32 bit JRE on a 64 bit machine, you'll get x64, so you're not knowing if you are on a 64bit architecture, so should invoke the 32bit version - but the other way round you can safely call the 64 bit version of your native application).

Another way might be brute forcing: Why now always trying eto run the 64 bit version, and in case of an error, use the 32 bit binary instead?

MRalwasser
+2  A: 

Here are some relevant System properties with the values on my machine:

os.arch:                 x86
os.name:                 Windows Vista
sun.arch.data.model:     32

From this information, you should be able to guess something like this:

String osName     = System.getProperty("os.name").toLowerString();
String dataModel  = System.getProperty("sun.arch.data.model");

boolean isWindows = osName.startsWith("windows");
boolean isLinux   = osName.startsWith("linux"); // or whatever
boolean is32bit   = "32".equals(dataModel);
boolean is64bit   = "64".equals(dataModel);

if(isWindows && is32bit){
    call32BitWindowsLib();
}

//etc.

For a completer reference, see:

seanizer
sun.arch.data.model ist not part of the vm specification and may not be present in jre of other vendors (e.g. ibm).Please note, that dataModel may also be "unknown"
MRalwasser
a) I'm pretty sure every other VM vendor will have a similar property, so it's just a matter of researching them and using the correct property based on the value of the `java.vendor` system property. b) "unknown": that's way I coded it defensively using `"32".equals(dataModel)` etc.
seanizer