在java程序中使用c ++ Dll [英] use c++ Dll in java program
问题描述
我正在尝试使用 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。*;
/ **
*由Amit Baz于06/06/2016创建。
* /
public class UseDllInJava {
static {
System.loadLibrary(SimpleDll);
}
public native static void HelloFromCPP();
public static void main(String [] args){
HelloFromCPP();
}
}
这是dll: p>
SimpleDll.h
#pragma once
命名空间SimpleDll
{
class MyFunctions
{
public:
static __declspec(dllexport)void HelloFromCPP();
};
}
SimpleDll.cpp:
#includeSimpleDll.h
#include< stdio.h>
#include< iostream>
使用namespace std;
命名空间SimpleDll {
externC{
void MyFunctions :: HelloFromCPP(){
cout<< 你好,从cpp<< ENDL;
}
}
}
我还添加了变量
-Djava.library.path =Path\To\SimpleDll到Intellij运行配置中的VM选项。 >
但是当我运行该程序时,会返回错误:
线程中的异常mainjava.lang.UnsatisfiedLinkError:
UseDllInJava.HelloFromCPP()V
编辑
现在正常工作!
来自本教程: https://www3.ntu.edu.sg /home/ehchua/programming/java/JavaNativeInterface.html
此外,我不知道JNI命名约定是 Java_< ; Package_name> _< Class_name> _< Function_name>
此线程如何修复不满意的链接错误(在JNI项目中找不到依赖库)他们也在讨论 java.lang.UnsatisfiedLinkError
I'm trying to use the System.LoadLibrary()
to use a simple dll i wrote in c++.
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();
}
}
This is the 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;
}
}
}
I also added the variable
-Djava.library.path="Path\To\SimpleDll" to the VM options in the Intellij run configurations.
But when I run the program it returns the error:
Exception in thread "main" java.lang.UnsatisfiedLinkError: UseDllInJava.HelloFromCPP()V
EDIT
Now it works!
I got help from this tutorial: https://www3.ntu.edu.sg/home/ehchua/programming/java/JavaNativeInterface.html
Also I didn't know that the JNI naming convention is Java_<Package_name>_<Class_name>_<Function_name>
Check this thread How to fix an UnsatisfiedLinkError (Can't find dependent libraries) in a JNI project they are also discussing java.lang.UnsatisfiedLinkError
这篇关于在java程序中使用c ++ Dll的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!