1

我正在尝试调用其中一种本机方法,但我得到“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 方法)时,它会失败。

4

1 回答 1

2

您的 Java 类的名称是CLS_NativeData. 但是,在 C++ 中,您编写:

Java_pkg_1main_CLS_1NativeData_getValue

CLS_1NativeData在任何地方使用。它拼写错误。所以类加载器在你的共享库中找不到本地方法并说java.lang.UnsatisfiedLinkError

于 2013-08-11T16:11:28.340 回答