Jackie
Jackie

Reputation: 23529

java.lang.UnsatisfiedLinkError: Native method not found When using header

I have the following:

jniinterface.h

#include <jni.h>
extern "C" {
  JNIEXPORT jdouble JNICALL Java_com_gleason_finance_JniLib_init(JNIEnv * env, jobject obj, jdouble SO, jdouble U, jdouble D, jdouble R);
};

jniinterface.cpp

#include "jniinterface.h"
JNIEXPORT jdouble JNICALL Java_com_gleason_finance_JniLib_init(JNIEnv * env, jobject obj, jdouble SO, jdouble U, jdouble D, jdouble R, jint N, jdouble K)
{
  return 0.0;
}

But this returns the following:

E/AndroidRuntime( 3638): java.lang.UnsatisfiedLinkError: Native method not found: com.me.finance.JniLib.init:(DDDDID)D

It works fine if I change jniinterface.cpp to:

#include <jni.h>
extern "C" JNIEXPORT jdouble JNICALL Java_com_gleason_finance_JniLib_init(JNIEnv * env, jobject obj, jdouble SO, jdouble U, jdouble D, jdouble R, jint N, jdouble K)
{
  return 0.0;
}

It works, I am kind of new to C++ so am I doing something wrong? Should I just remove the header? Why doesn't it work with the header?

Not sure why this will help since it is clearly a C++ issue (because of the C fix) but here:

public class JniLib {
  static {
    System.loadLibrary("fin");
  }
  public static native double init(double SO, double U, double R, double D, int N, double k);
}

Upvotes: 1

Views: 4100

Answers (1)

krsteeve
krsteeve

Reputation: 1804

In jniinterface.h, the return type of the function is void. It needs to be jdouble! You can leave jniinterface.cpp as it was in the first snippet.

Edit: You are also missing two parameters in the header file. (named N and K)

#include <jni.h>
extern "C" {
  JNIEXPORT jdouble JNICALL Java_com_gleason_finance_JniLib_init(JNIEnv * env, jobject obj, jdouble SO, jdouble U, jdouble D, jdouble R, jint N, jdouble K);
};

Whenever you see an error like this, the first thing you should do is carefully go over the method signatures - name, package, parameter and return type. Any small mistake leads to this general error!

Upvotes: 2

Related Questions