0

I am trying to call native methods from a JAVA class. This JAVA class after I compiled it and created the [.h] version for it and details all the definition within the [.c] file then after.The next step followed creation of [.dll] file that I loaded with the JAVA class within the static parameters.

Now when I try to run my program, it gives me the following error :

Exception in thread "main" java.lang.UnsatisfiedLinkError: sample.HelloWorld.print()V

I don't understand as to how to map the native methods from my JAVA program?

More explanation with code :

I have a [HelloWorld.java] class file that has the native method called [print()] method declaration :

package sample;
public class HelloWorld{
    public native void print();
    static{
        System.loadLibrary("HelloWorld");
    }
}

Now, on compilation, I get the [.class] file version of it. The next I created the [.h] and [.c] file having the definition for the native method [print()] as follows :

HellWorld.c

#include<stdio.h>
#include<jni.h>
#include<windows.h>
#include "HelloWorld.h"
JNIEXPORT void JNICALL Java_HelloWorld_print
  (JNIEnv * env, jobject jobj){
          printf("Hello there!!!!!!");
          return;          
  }

Now when I build this [.c] file, it generates the required [.dll] file that is needed. Everything works just fine with no errors and the message gets printed of print() method when I invoke the JAVA file.

The main problem arises when I try to call this print() method in HelloWorld.java class file from different class, Invoker.java class file. Then this generates the exception :

Exception in thread "main" java.lang.UnsatisfiedLinkError: sample.HelloWorld.print()V

I do not get why this happens so when I do it? Any suggestions?

Abhishek
  • 1,974
  • 5
  • 31
  • 67

1 Answers1

0

the JNICALL function name in the .c file should be exact as that in the HelloWorld.h file

see this link for more info

Anuswadh
  • 542
  • 1
  • 11
  • 19