use*_*979 2 java java-native-interface native mingw
每次运行程序时,我都会收到java.lang.UnsatisfiedLinkError错误.我有一个本机,一个包装器,以及通过包装器调用本机的程序.
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
Run Code Online (Sandbox Code Playgroud)
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;
}
Run Code Online (Sandbox Code Playgroud)
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();
}
}
Run Code Online (Sandbox Code Playgroud)
Main.Java
public class Main
{
public static void main(String[] args)
{
Wrapper.MessageBox("Testing JNI", "JNI");
}
}
Run Code Online (Sandbox Code Playgroud)
本机是使用MinGW 64位构建的.无论如何,我不知道为什么我得到错误.救命?????
Zax*_*Zax 10
我认为问题是你的JNI签名不匹配.那是:
JNIEXPORT void JNICALL native_MessageBox(string text, string title);
Run Code Online (Sandbox Code Playgroud)
应该是这样的:
JNIEXPORT void JNICALL java_com_example_Wrapper_native_MessageBox(string text, string title);
Run Code Online (Sandbox Code Playgroud)
其中,java_com_example应替换为您的包名(.将在包名中替换为_).
要么
我建议你使用java中提供的javah -jni选项生成本机函数签名和声明.
归档时间: |
|
查看次数: |
25255 次 |
最近记录: |