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, b开发者_开发百科ut not sure how to get determine if it is 32-bit or 64-bit platform?
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:
- System.getProperties()
- When writing Java code, how do I distinguish between 32 and 64-bit operation?
- How can I tell if I'm running in 64-bit JVM or 32-bit JVM?
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?
精彩评论