UnsatisfiedLinkError in JNI Code

孤人 提交于 2019-12-12 01:13:20

问题


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

回答1:


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.



来源:https://stackoverflow.com/questions/21569400/unsatisfiedlinkerror-in-jni-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!