eouti
eouti

Reputation: 5658

JNI : java.lang.UnsatisfiedLinkError error

I have some problems trying to get JNI working. I have a C++ application and I want to call methods from this application in my Java program. I wrote my Java program calling native methods, then I used javah to generate the header file. Once my C++ test program is compiled into a library .so, I call this library from my java program. Loading System.loadLibrary is ok, but it seems that he can't find the methods in it. I get a :

Exception in thread "main" java.lang.UnsatisfiedLinkError: ApiPackets.get_lost_packets()V
    at ApiPackets.get_lost_packets(Native Method)
    at ApiPackets.main(ApiPackets.java:12)

Here's my Java calling native methods :

public class ApiPackets {

    public native void get_lost_packets();
    public native int get_late_packets();
    public native int get_out_of_order_packets();

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

    public static void main(String[] args) {
        ApiPackets api = new ApiPackets();
        System.out.println("pass");
        api.get_lost_packets();
}
}

And here's my C++, then compiled into libApiPackets.so:

#include "ApiPackets.h"
#include <stdio.h>
#include "main_window.h"

JNIEXPORT void JNICALL Java_ApiPackets_get_lost_packets
  (JNIEnv * env, jobject obj) {
    printf("coucou");
    return;
}

JNIEXPORT jdouble JNICALL Java_ApiPackets_get_1late_1packets
  (JNIEnv * env, jobject obj) {
    jdouble late = mw->priv->current_call->get_late_packets ();
    return late;
}

JNIEXPORT jdouble JNICALL Java_ApiPackets_get_1out_1of_1order_1packets
  (JNIEnv * env, jobject obj) {
    jdouble out_of_order = mw->priv->current_call->get_out_of_order_packets ();
    return out_of_order;
}

Upvotes: 1

Views: 1624

Answers (1)

James Kanze
James Kanze

Reputation: 154047

The C++ functions must be declared extern "C". (The JNI macro JNIEXPORT doesn't do this, since it is designed to be used in both C and C++.)

Upvotes: 1

Related Questions