Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mac + jni + java

A little background: I have a java application that needs to talk to a third party hardware on mac. They have given me the sdk but it is not in Java. So I am trying to make jnilib that will act as a bridge between my java application and the SDK.

The issue: I have made a small sample jnilib that talks to the SDK but when I try to use it in my java program I get the following error

Exception in thread "main" java.lang.UnsatisfiedLinkError: /Users/john.doe/Desktop/eclipse/workspace/Lesson13_Jni_Smart7/bin/libSmartTest7.jnilib:  Library not loaded: build/Release/SMARTResponseSDK.framework/Versions/A/SMARTResponseSDK  Referenced from: /Users/john.doe/Desktop/eclipse/workspace/Lesson13_Jni_Smart7/bin/libSmartTest7.jnilib      
Reason: image not found
    at java.lang.ClassLoader$NativeLibrary.load(Native Method)
    at java.lang.ClassLoader.loadLibrary0(ClassLoader.java:1827)
    at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1742)
    at java.lang.Runtime.loadLibrary0(Runtime.java:823)
    at java.lang.System.loadLibrary(System.java:1045)
    at com.learning.lesson13.JniSmart7.<clinit>(JniSmart7.java:6)

From the error it looks like my libSmartTest7.jnilib is looking for the library SMARTResponseSDK.

What I have tried I know where the library SMARTResponseSDK is on my Mac. I tried copying it over to my working folder in eclipse but I still get the error. I have tried using the -DJava.library.path but I still get the error.

Any ideas on what the best possible approach would be.

like image 915
user1216750 Avatar asked Nov 13 '22 10:11

user1216750


1 Answers

Once you are inside JNI code, it no longer matters what java.library.path points at.

Once you are inside JNI code, all you can do is to make sure library is visible to your code via LD_LIBRARY_PATH/DYLD_LIBRARY_PATH, or you can dynamically load your library file from any location you like.

So, for you, I suggest to take a look here:

  • dynamic loading of library in JNI - http://jnicookbook.owsiak.org/recipe-No-018/
  • Calling code from shared library (adapter pattern) - http://jnicookbook.owsiak.org/recipe-No-023/

You can also benefit from compilation flags while building your JNI library and use rpath.

like image 142
Oo.oO Avatar answered Nov 16 '22 04:11

Oo.oO