javacnetbeansjava-native-interfacenative-methods

JNI UnsatisfiedLink Error


So I'm new to JNI and I'm following a simple hello word example but I keep getting error UnsatisfiedLinkError. What am I doing wrong? Here's my .h file:

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

#ifndef _Included_JNITEST_jnihellonative
#define _Included_JNITEST_jnihellonative
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class:     JNITEST_jnihellonative
* Method:    hellofromc
* Signature: ()V
*/
JNIEXPORT void JNICALL Java_JNITEST_jnihellonative_hellofromc(JNIEnv *,jobject);

#ifdef __cplusplus
}
#endif
#endif

.c File

#include <jni.h>
#include<stdio.h>
#include<windows.h>
#include "jnihellonative.h"

JNIEXPORT void JNICALL
Java_JNITESTS_jnihellonative_hellofromc(JNIEnv *env, jobject obj){
    printf("Hello World");
    return;
  }

java main class

package JNITEST;


public class Jnihello {


   public static void main(String[] args) {
        jnihellonative jniprint = new jnihellonative();
        jniprint.hellofromc();
   }

}

java class

package JNITEST;


public class jnihellonative {

    public native void hellofromc();

    static{
        System.load("C:\\Users\\Kevin\\Documents\\NetBeansProjects\\JniHelloTest.dll");
    }
}

I keep getting this error

Exception in thread "main" java.lang.UnsatisfiedLinkError: JNITEST.jnihellonative.hellofromc()V
    at JNITEST.jnihellonative.hellofromc(Native Method)
    at JNITEST.Jnihello.main(Jnihello.java:19)
Java Result: 1
BUILD SUCCESSFUL (total time: 1 second)

I have tried using System.load() and System.loadLibrary() but i get same error.


Solution

  • You've changed the name of your class since you generated the .h file and wrote the .c file. The .h file has jnihellonative, your Java code has Jnihello.

    I have tried using System.load() and System.loadLibrary()

    Irrelevant. You're not getting the exception from either of these, you're getting it when calling your native method.