Why I get: java.lang.UnsatisfiedLinkError

Java Code:

package Package; public class IntArray { private native int sumArray(int[] arr); public static void main(String args[]) { IntArray p = new IntArray(); int arr[] = new int[10]; for(int i=0 ; i<10 ; i++) { arr[i] = i; } int sum = p.sumArray(arr); // pass the array to the c function sumArray System.out.println("Sum is : " + sum); } static { // 17th statement System.loadLibrary("IntArray"); } } 

C code:

 #include<stdio.h> #include "Package_IntArray.h" jint Java_Package_IntArray_sumArray(JNIEnv *env, jobject obj, jintArray arr) { jint buf[10]; jint i, sum = 0; (*env)->GetIntArrayRegion(env, arr, 0, 10, buf); for (i = 0; i < 10; i++) { sum += buf[i]; } return sum; } 

Then run the command:

 java -Djava.library.path=W:\elita\jnitesters\workspace\c\IntArray\bin\Debug Package.IntArray 

to which I get the following exceptions:

 Exception in thread "main" java.lang.UnsatisfiedLinkError: Package.IntArray.sumArray([I)I at Package.IntArray.sumArray(Native Method) at Package.IntArray.main(IntArray.java:17) 

There is a dll file named IntArray.dll in the W: \ elita \ jnitesters \ workspace \ c \ IntArray \ bin \ Debug directory

+7
source share
3 answers

Everything is good! But there is one thing you should try. Permanently delete all kinds of files (including a DLL file) created by IDE :: blocks code. And then rebuild your program. Drop MACRO , if there is one! Then it should work.

0
source

Hmmm, I don't see anything obvious, but a few things to try:

1) Specify the full path in System.load() stack trace is a bit confused in that it indicates that sumArray () is called on the 17th line, which is static, so I don't know what to do with it.

2) Put a try / catch block around your System.load()

3) Try defining your method as follows:

  JNIEXPORT void JNICALL Java_Package_IntArray_sumArray(JNIEnv *env, jobject obj, jintArray arr) { /*Implement Native Method Here*/ } 

Perhaps this method is not exported to your DLL file, so it does not find it.

0
source

You have already created the elita folder under the lib forlder of your Netbeans project. Copy your .dll to this folder. Now in the NetBeans IDE, right-click and select Project Properties-> Run Settings-> VM: Djava.library.path=lib/elita

Then run your program from the IDE itself.

0
source

All Articles