mafioso mafioso - 1 year ago 75
C Question

Link Error in Java

I get the following error:

Exception in thread "main" java.lang.UnsatisfiedLinkError: model.JNIResultSet.getSpieler()[Lmodel/Spieler;
at model.JNIResultSet.getSpieler(Native Method)
at model.JNIResultSet.main(

My java classes look like this:

package model;

public class Spieler {
private String vorname;
private String nachname;
private int trikotnummer;

public Spieler(String vorname, String nachname, int trikotnummer) {
this.vorname = vorname;
this.nachname = nachname;
this.trikotnummer = trikotnummer;


package model;

public class JNIResultSet {

public JNIResultSet() { }

public native Spieler[] getSpieler();

static {

public static void main(String[] args) {
JNIResultSet jni = new JNIResultSet();
Spieler s[] = jni.getSpieler();
System.out.println("Laenge: " + s.length);


My header file looks like that:

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

#ifndef _Included_model_JNIResultSet
#define _Included_model_JNIResultSet
#ifdef __cplusplus
extern "C" {
* Class: model_JNIResultSet
* Method: getSpieler
* Signature: ()[Lmodel/Spieler;
JNIEXPORT jobjectArray JNICALL Java_model_JNIResultSet_getSpieler
(JNIEnv *, jobject);

#ifdef __cplusplus

And finally my C code looks like that:

JAVA_model_JNIResultSet_getSpieler(JNIEnv *env, jobject obj) {

Spieler *ptr = head;
jobjectArray ret;
int i;
jclass clazz = (*env)->FindClass(env, "model/Spieler");
jmethodID mid = (*env)->GetMethodID(env, clazz, "<init>", "(III)V");
jobject object;

ret= (*env)->NewObjectArray(env, count,

for(i = 0; i < count; i++) {
object = (*env)->NewObject(env, clazz, mid, ptr);
ret, i, object);
ptr = ptr->next;

return ret;

Do you have any suggestions how to fix that problem?

I also don't know if the C code is right, maybe that's the problem?

I execute it this way:

cd src
javac model/
cd ..

cd bin
javah -jni model.JNIResultSet

gcc -fPIC -o -shared -I/usr/java/jdk1.8.0_73/include/ SharedTable.c -lc

java -Djava.library.path=/home/pupil/workspace/JNITableV2/bin/ model.JNIResultSet

Probably this is the error I've updated the C code but I think it's wrong:

jclass clazz = (*env)->FindClass(env, "model/JNIResultSet");
jmethodID mid = (*env)->GetMethodID(env, clazz, "getSpieler", "()Lmodel/Spieler;");

nm -D

U atoi

0000000000202088 B bss_start

00000000002020b0 B current

w __cxa_finalize

0000000000202088 D _edata

00000000002022b8 B _end

U fclose

0000000000000ef0 T _fini

U fopen

w __gmon_start

00000000002020a8 B head

0000000000000948 T _init

0000000000000b35 T insertFirst

U _IO_getc

w _ITM_deregisterTMCloneTable

w _ITM_registerTMCloneTable

0000000000000ca2 T JAVA_model_JNIResultSet_getSpieler

w _Jv_RegisterClasses

U malloc

U memcpy

U memset

0000000000000c49 T reverse

U strcpy

00000000002020c0 B string

U strlen

Answer Source

getSpieler() method could be declared static; you can use it without instantiating JNIResultSet class.

mid should point to the constructor, not getSpieler(), but the <init> signature is wrong: it should take Ljava/lang/String;Ljava/lang/String;I and not three ints:

jmethodID mid = (*env)->GetMethodID(env, clazz, "<init>", "(Ljava/lang/String;Ljava/lang/String;I)V");
jobject strVorname = (*env)->NewStringUTF(env, ptr->vorname);
jobject strNachname = (*env)->NewStringUTF(env, ptr->nachname);
object = (*env)->NewObject(env, clazz, mid, strVorname, strNachname, ptr->trikotnummer);

Always check the method IDs and objects for NULL before proceeding to the next step.

Regarding the unsatisfied link, check that the file is good: run nm -D to see if JAVA_model_JNIResultSet_getSpieler is actually exported. rename JAVA_model_JNIResultSet_getSpieler to Java_model_JNIResultSet_getSpieler, exactly as indicated in the generated .h file.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download