JNI: Library is Found on Path, but Method is not (java.lang.UnsatisfiedLinkError)

后端 未结 5 785
南笙
南笙 2021-01-12 15:43

I\'m trying to use JNI and getting java.lang.UnsatisfiedLinkError. Unlike the other million questions asked about this, I have the lib on my path, and have even seen the ex

5条回答
  •  萌比男神i
    2021-01-12 15:50

    Try with following example for Windows: (remember that the Java class name must be the same that corresponding file name)

    Step 1. Create the following Java file (P.java):

    class P
    {
      static
      {
        // "P" is the name of DLL without ".dll"
        System.loadLibrary ("P");
      }
    
      public static native void f(int i);
    
      public static void main(String[] args)
      {
        f(1);
      }
    }
    

    Step 2. javac P.java

    Step 3. javah P

    Then, "javah" generates the header file "P.h"

    Step 4. Create the file "P.def" including the following two lines (this file defines the exported symbols, in this case the name of C function):

    EXPORTS
    Java_P_f
    

    Step 5. Create your C file (P.c):

    #include "P.h"
    
    JNIEXPORT void JNICALL Java_P_f(JNIEnv *env, jclass c, jint i)
    {
      printf("%i\n",i);
    }
    

    Step 6. Within Visual Studio command promt, define the following variables:

    set JAVA_HOME= the path of JDK

    set include=%include%;%JAVA_HOME%\include;%JAVA_HOME%\include\win32

    Step 7. Generate DLL:

    cl /LD P.c P.def

    Step 8. Run the Java program:

    java P

    (Note: P.dll and P.class are located in the same directory)

提交回复
热议问题