Ahmed Ebaid
Ahmed Ebaid

Reputation: 417

UnsatisfiedLinkError with JNI

.h file

#include <jni.h>
#include "NativePackage_HelloWorld.h"
#include <stdio.h>

JNIEXPORT void JNICALL Java_NativePackage_HelloWorld_displayHelloWorld(JNIEnv *env, jobject obj)   {
printf("Hello world!\n");
return;
}

Java

package NativePackage;

public class HelloWorld {
public native void displayHelloWorld();

static {
    System.loadLibrary("hello");
}

public static void main(String[] args) {
    new HelloWorld().displayHelloWorld();
}
}

.c file

#include <jni.h>
#include "NativePackage_HelloWorld.h" 
#include <stdio.h>

JNIEXPORT void JNICALL Java_NativePackage_HelloWorld_displayHelloWorld(JNIEnv *env, jobject obj){
printf("Hello world!\n");
return;
}

I've compiled my .c file using

gcc -I /Library/Java/JavaVirtualMachines/jdk1.8.0_20.jdk/Contents/Home/include/ -I  /Library/Java/JavaVirtualMachines/jdk1.8.0_20.jdk/Contents/Home/include/darwin/ -fPIC -o hello -c   HelloWorldImp.c 

However, trying to run using java NativePackage/HelloWorld produce the following error:

java  -Djava.library.path=NativePackage/ NativePackage/HelloWorld

Exception in thread "main" java.lang.UnsatisfiedLinkError: no hello in java.library.path
at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1857)
at java.lang.Runtime.loadLibrary0(Runtime.java:870)
at java.lang.System.loadLibrary(System.java:1119)
at NativePackage.HelloWorld.<clinit>(HelloWorld.java:7)

I'm running on a MAC OS X 10.10

Upvotes: 4

Views: 2988

Answers (3)

Heinrich
Heinrich

Reputation: 398

For an updated answer for macOS Monterey 12.6.1

HelloWorld.c

#include <jni.h>        // JNI header provided by JDK
#include <stdio.h>      // C Standard IO Header
#include "HelloWorld.h"   // Generated
 
// Implementation of the native method sayHello()
JNIEXPORT void JNICALL Java_HelloWorld_someNativeMethod(JNIEnv *env, jobject thisObj) {
   printf("Hello World!\n");
   return;
}

HelloWorld.java

class HelloWorld {
    private native void someNativeMethod();
    public static void main(String[] args) {
        new HelloWorld().someNativeMethod();
    }
    static {
        // must be word between lib and .dylib, e.g. libMyNative.dylib
        System.loadLibrary("MyNative");
    }
}

Run

# compile java - generates HelloWorld.h and HelloWorld.class
javac -h . HelloWorld.java

# compile native c files
gcc -I "$JAVA_HOME/include" -I "$JAVA_HOME/include/darwin" -dynamiclib -o libMyNative.dylib -shared HelloWorld.c

# run all
java -Djava.library.path=. HelloWorld

Tested on macOS Monterey 12.6.1

$ echo $JAVA_HOME
/Library/Java/JavaVirtualMachines/amazon-corretto-17.jdk/Contents/Home

Upvotes: 0

Ahmed Ebaid
Ahmed Ebaid

Reputation: 417

Seems like things worked with using the correct naming scheme for OS X libhello.dylib and the -shared option.

gcc -I /Library/Java/JavaVirtualMachines/jdk1.8.0_20.jdk/Contents/Home/include/ -I /Library/Java/JavaVirtualMachines/jdk1.8.0_20.jdk/Contents/Home/include/darwin/  -o libhello.dylib -shared HelloWorldImp.c

Upvotes: 4

girish946
girish946

Reputation: 745

Try using

System.load("/home/project/lib/libhello.so");

give the absolute path for the library.

that helped me when i had the same problem.

Upvotes: 5

Related Questions