我试图使用System.LoadLibrary()
来使用我在c ++中编写的简单dll。
UseDllInJava.java:
import com.sun.jna.Library;
import com.sun.jna.Native;
import com.sun.jna.NativeLong;
import com.sun.jna.Platform;
import com.sun.jna.*;
/**
* Created by Amit Baz on 06/06/2016.
*/
public class UseDllInJava {
static{
System.loadLibrary("SimpleDll");
}
public native static void HelloFromCPP();
public static void main(String[] args){
HelloFromCPP();
}
}
这是dll:
SimpleDll.h
#pragma once
namespace SimpleDll
{
class MyFunctions
{
public:
static __declspec(dllexport) void HelloFromCPP();
};
}
SimpleDll.cpp:
#include "SimpleDll.h"
#include <stdio.h>
#include <iostream>
using namespace std;
namespace SimpleDll {
extern "C" {
void MyFunctions::HelloFromCPP() {
cout << "Hello from cpp" << endl;
}
}
}
我还添加了变量
-Djava.library.path =&#34;路径\至\ SimpleDll&#34;到Intellij运行配置中的VM选项。
但是当我运行程序时,它会返回错误:
线程中的异常&#34; main&#34; java.lang.UnsatisfiedLinkError中: UseDllInJava.HelloFromCPP()V
修改
现在有效!
我从本教程获得了帮助:https://www3.ntu.edu.sg/home/ehchua/programming/java/JavaNativeInterface.html
此外,我还不知道JNI命名约定是Java_<Package_name>_<Class_name>_<Function_name>
答案 0 :(得分:0)
检查此帖子How to fix an UnsatisfiedLinkError (Can't find dependent libraries) in a JNI project他们也在讨论java.lang.UnsatisfiedLinkError