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

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

问题描述

每次运行程序时,我都会不断收到 java.lang.UnsatisfiedLinkError 错误.我有一个本机,一个包装器和一个通过包装器调用本机的程序.

I keep getting a java.lang.UnsatisfiedLinkError error every time I run my program. I have a native, a wrapper, and the program to call the native through the wrapper.

main.h

#ifndef __MAIN_H__
#define __MAIN_H__

#include <windows.h>
#ifdef BUILD_DLL
    #define DLL_EXPORT __declspec(dllexport)
#else
    #define DLL_EXPORT __declspec(dllimport)
#endif

#include<jni.h>
#include<iostream>

using namespace std;

extern "C"
{

JNIEXPORT void JNICALL native_MessageBox(string text, string title);

}

#endif

main.cpp

#include "main.h"

#include<windows.h>
#include<iostream>

using namespace std;

JNIEXPORT void JNICALL MsgBox(string text, string title)
{
    MessageBox(NULL, text.c_str(), title.c_str(), MB_OK);
}

extern "C" DLL_EXPORT BOOL APIENTRY DllMain(HINSTANCE hinstDLL, DWORD fdwReason,
LPVOID lpvReserved)
{
    switch (fdwReason)
    {
        case DLL_PROCESS_ATTACH:
            break;

        case DLL_PROCESS_DETACH:
            break;

        case DLL_THREAD_ATTACH:
            break;

        case DLL_THREAD_DETACH:
            break;
    }
    return TRUE;
}

Wrapper.java

public class Wrapper
{
   private static File nativeFile = null;

       static
   {
            //ArchitectureReader and OSReader are classes I made to read the CPU              
            //bits and the OS
    ArchitectureType archType = ArchitectureReader.getArcitecture();
    OSType osType = OSReader.getOS();

    String filename = "C:/native-";

    if (osType == OSType.Windows)
    {
        if (archType == ArchitectureType.BITS_32)
            filename += "win32";
        else if (archType == ArchitectureType.BITS_64)
            filename += "win64";
        else
            System.exit(1);
    }
    else
        System.exit(1);

    nativeFile = new File(filename + ".dll");
    if (!nativeFile.exists())
        System.exit(1);

    System.load(filename); //This is where the Exception is thrown
}

private static native void native_MessageBox(String text, String title);
public static void MesageBox(String text, String title)
{
    native_MessageBox(text, title);
}

public static String getNativePath()
{
    return nativeFile.getAbsolutePath();
}
}

Main.Java

public class Main
{
public static void main(String[] args)
{
    Wrapper.MessageBox("Testing JNI", "JNI");
}
}

本机是使用MinGW 64位构建的.无论如何,我不明白为什么会得到错误.帮助??????

The native was built using MinGW 64 bit. Anyway, I don't get why I get the error. Help?????

推荐答案

我认为问题是您的JNI签名不匹配. 那就是:

I think the problem is that your JNI signature doesn't match. That is:

JNIEXPORT void JNICALL native_MessageBox(string text, string title);

应该是这样的:

JNIEXPORT void JNICALL java_com_example_Wrapper_native_MessageBox(string text, string title);

其中,应将java_com_example替换为您的包名称(包名称中的_替换为.).

where, java_com_example should be replaced with your package name (. to be replace with _ in package name).

OR

我建议您使用Java中提供的 javah -jni 选项生成本机函数签名和声明.

I would suggest you to generate your native function signature and declaration using javah -jni option available in java.

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

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