用Java加载32或64位JNI库 [英] Loading 32 or 64 bit JNI Library in Java

查看:145
本文介绍了用Java加载32或64位JNI库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
Java:在dll之间切换取决于系统架构(32/64)

Possible Duplicate:
Java : switching between dll depends on system architecture (32/64)

在我的Java应用程序中,我需要一个JNI库,该库可用于32位和64位.我不想交付两个不同版本的应用程序,所以我希望同时为应用程序和两个库一起交付,并且应用程序必须自行确定要加载哪个库(foobar32.so或foobar64.so).我该怎么办?

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.

是否可以检查某些系统属性来确定是否使用32位Java或64位Java来运行我的应用程序?我知道有一些os.arch属性,但是根据

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.

那么让应用程序决定是加载32位还是64位JNI库的最佳方法是什么?

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

推荐答案

为什么无法预测?您只想检测它是否为32位系统,在这种情况下,它将始终为x86(当然,对于x86处理器而言),其他所有内容都表示64位.

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.

如果您知道您的代码也可以在PPC或其他非x86处理器上执行,则问题开始.但是在这种情况下,您可能会使用带有异常驱动流控制的丑陋技巧,并使用try {} catch()来检测"此类情况.

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.

恕我直言,os.arch将是最好的解决方案.

IMHO os.arch will be the best solution.

这篇关于用Java加载32或64位JNI库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆