JNI java.lang.UnsatisfiedLinkError [英] JNI java.lang.UnsatisfiedLinkError

查看:181
本文介绍了JNI java.lang.UnsatisfiedLinkError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图调用其中一个本机方法,但我得到java.lang.UnsatisfiedLinkError。

I'm trying to call one of the native methods but i get "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头文件

C/C++ DLL Header File

/* 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方法),它失败。

So, when i call the first method, it works perfectly, but when i call the second method (void method), it fails.

推荐答案

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

The name of your Java class is CLS_NativeData. However, in C++ you write:

Java_pkg_1main_CLS_1NativeData_getValue

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

And use CLS_1NativeData everywhere. It is misspelled. So the class loader cannot find the native method in your shared library and says java.lang.UnsatisfiedLinkError.

这篇关于JNI java.lang.UnsatisfiedLinkError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆