JNI java.lang.UnsatisfiedLinkError

时间:2013-08-11 15:55:07

标签: java c++ java-native-interface unsatisfiedlinkerror

我正在尝试调用其中一个本机方法,但我得到“java.lang.UnsatisfiedLinkError”。

Java类:

public class CLS_NativeData {

    private final static String sLibName = "NativeData.dll";

    private native String getValue(int i);
    private native void setValue(int i);

    public CLS_NativeData() {
        super();
    }

    static {
        System.load(CLS_Globals.LIB_PATH + sLibName);
    }

    public String getData(int i) {
        return getValue(i);
    }

    public void setData(int i) {
        setValue(i);
    }
}

C / C ++ DLL头文件

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

#ifndef _Included_pkg_main_CLS_NativeData
#define _Included_pkg_main_CLS_NativeData
#ifdef __cplusplus
extern "C" {
#endif
/*
 * Class:     pkg_main_CLS_NativeData
 * Method:    getValue
 * Signature: (I)Ljava/lang/String;
 */
JNIEXPORT jstring JNICALL Java_pkg_1main_CLS_1NativeData_getValue
  (JNIEnv *, jobject, jint);

/*
 * Class:     pkg_main_CLS_NativeData
 * Method:    setValue
 * Signature: (I)V
 */
JNIEXPORT void JNICALL Java_pkg_1main_CLS_1NativeData_setValue
  (JNIEnv *, jobject, jint);

#ifdef __cplusplus
}
#endif
#endif

C / C ++ DLL.cpp

   JNIEXPORT jstring JNICALL Java_pkg_1main_CLS_1NativeData_getValue
        (JNIEnv * env, jobject obj, jint i) 
    {
        CoInit();

        switch(i) {
        case 0:
            return env->NewStringUTF(getCPUInfo().c_str()); 
            break;
        case 1:
            return env->NewStringUTF(getSystemInfo().c_str()); 
            break;
        case 2:
            return env->NewStringUTF(getMachineInfo().c_str()); 
            break;
        case 3:
            return env->NewStringUTF(getAntivirusInfo().c_str()); 
            break;
        case 4:
            return env->NewStringUTF(getProcessList().c_str()); 
            break;
        default:
            return env->NewStringUTF("ERROR"); 
        }
    }

    // FAILED METHOD
    JNIEXPORT void JNICALL Java_pkg_1main_CLS_1NativeData_setValue
        (JNIEnv * env, jobject obj, jint i)
    {
        switch (i) {
        case 0:
            printf("Hola mundo\n");
            break;
        }
    }

所以,当我调用第一个方法时,它完美地工作,但是当我调用第二个方法(void方法)时,它会失败。

1 个答案:

答案 0 :(得分:2)

您的Java类名称为CLS_NativeData。但是,在C ++中你写道:

Java_pkg_1main_CLS_1NativeData_getValue

并在任何地方使用CLS_1NativeData。它拼写错误。因此,类加载器无法在共享库中找到本机方法,并说java.lang.UnsatisfiedLinkError