SamYan
SamYan

Reputation: 1571

JNI Error "java.lang.UnsatisfiedLinkError:"

I'm trying to call native methods through JNI libraries, but I get "java.lang.UnsatisfiedLinkError:" Now I will describe the steps I do.

test.java

package pkgmain;

    public class test {
        public native static int getDouble(int n);

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

        public static void main(String[] args) {
            for (int n = 1; n <= 20; n++) {
                System.out.println(n + " x 2 = " + getDouble(n));
            }
        }
    }

In CMD Console i tip:

javah -classpath . pkgmain.test

I get the generated c header file "pkgmain_test.h":

/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class pkgmain_test */

#ifndef _Included_pkgmain_test
#define _Included_pkgmain_test
#ifdef __cplusplus
extern "C" {
#endif
/*
 * Class:     pkgmain_test
 * Method:    getDouble
 * Signature: (I)I
 */
JNIEXPORT jint JNICALL Java_pkgmain_test_getDouble
  (JNIEnv *, jclass, jint);

#ifdef __cplusplus
}
#endif
#endif

Dll Code:

#include "pkgmain_test.h"

JNIEXPORT jint JNICALL Java_pkgmain_test_getDouble(JNIEnv *env,
           jclass clz, jint n) {
    return n * 2;
}

Then i compile the code using Dev C++. So far so good. Then i copy the compiled "test.dll" to my project and run it.

Result obtained:

Exception in thread "main" java.lang.UnsatisfiedLinkError: pkgmain.test.getDouble(I)I
    at pkgmain.test.getDouble(Native Method)
    at pkgmain.test.main(test.java:12)

I was looking at many tutorials and following all the steps but always get this error at the end.

What I'm doing wrong? Sorry for bad english and thanks in advance.

Upvotes: 1

Views: 1338

Answers (1)

SamYan
SamYan

Reputation: 1571

I got my error solution. Error was in dll project creation. The correct option is "in c" instead of default option "in c++" like in capture.

enter image description here

Now it works perfectly. Debug result:

1 x 2 = 2
2 x 2 = 4
3 x 2 = 6
4 x 2 = 8
5 x 2 = 10
6 x 2 = 12
7 x 2 = 14
8 x 2 = 16
9 x 2 = 18
10 x 2 = 20
11 x 2 = 22
12 x 2 = 24
13 x 2 = 26
14 x 2 = 28
15 x 2 = 30
16 x 2 = 32
17 x 2 = 34
18 x 2 = 36
19 x 2 = 38
20 x 2 = 40

Thanks anyway :)

Upvotes: 1

Related Questions