2013年2月28日木曜日

Loading 32 or 64 bit JNI Library in Java

In my Java application I need a JNI library which is available for 32 bit and 64 bit. I don't want to ship two different versions of my application so I want to ship the application with both libraries and the application must determine itself which library to load (foobar32.so or foobar64.so). How do I do that?

I thought about trying to load the first one and if this throws an exception then I load the second one but this sounds ugly.

Is there some system property I could check instead to determine if a 32 bit Java or a 64 bit Java is used to run my application? I know there is some os.arch property but the return value seems to be pretty unpredictable according to this answer.

So what is the best way to let the application decide if to load the 32 or 64 bit JNI library?

==>

Why unpredictable? You want only to detect if it is 32 bit system, and in this case it will be always x86 (of course for x86 processors), everything else means 64 bit.

Problems starts if you know that your code may be executed also on PPC or other non x86 processors. But in this case you may use ugly hack with exception driven flow control, and use try {} catch () to "detect" such situations.

IMHO os.arch will be the best solution.

0 件のコメント:

コメントを投稿