zaloo
zaloo

Reputation: 919

UnsatisfiedLinkError in JNI Code

I'm trying to create a simple JNI project to get the hang of JNI, but I keep running into this error:

Exception in thread "main" java.lang.UnsatisfiedLinkError: HPAProgram.sayHello()

I don't have much code yet, so I can paste most of it here.

I run the following commands:

javac HPAProgram.java
javah HPAProgram
cc -v -c -fPIC -I/System/Library/Frameworks/JavaVM.framework/Versions/A/Headers/ HPAProgram.c++ -o libHPAProgram.o
libtool -dynamic -lSystem libHPAProgram.o -o libHPAProgram.dylib
LD_LIBRARY_PATH=.
export LD_LIBRARY_PATH
java HPAProgram

HPAProgram.java

public class HPAProgram {

    public native void sayHello();

    public static void main(String[] args) {

        System.loadLibrary("HPAProgram");
        System.out.println("In java main");

        HPAProgram s = new HPAProgram();
        s.sayHello();
    }
}

HPAProgram.c++:

/*
 * HPAProgram.c++
 *
 *  Created on: Feb 4, 2014
 *      Author: zalbhathena
 */

//#include <jni.h>
#include <stdio.h>
#include "HPAProgram.h"

JNIEXPORT void JNICALL Java_JniSample_sayHello (JNIEnv *env, jobject obj) {
   printf("Hello World!\n");
}

HPAProgram.h:

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

#ifndef _Included_HPAProgram
#define _Included_HPAProgram
#ifdef __cplusplus
extern "C" {
#endif
/*
 * Class:     HPAProgram
 * Method:    sayHello
 * Signature: ()V
 */
JNIEXPORT void JNICALL Java_HPAProgram_sayHello
  (JNIEnv *, jobject);

#ifdef __cplusplus
}
#endif
#endif

Upvotes: 0

Views: 77

Answers (1)

user207421
user207421

Reputation: 310840

It said

java.lang.UnsatisfiedLinkError: HPAProgram.sayHello()

You have:

JNIEXPORT void JNICALL Java_JniSample_sayHello (JNIEnv *env, jobject obj)

and

JNIEXPORT void JNICALL Java_HPAProgram_sayHello(JNIEnv *, jobject);

Fix the .c file to agree with the .h file.

Upvotes: 2

Related Questions