在java程序中使用c ++ dll [英] use c++ Dll in java program

查看:159
本文介绍了在java程序中使用c ++ dll的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用 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 on 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 
$ b b namespace SimpleDll
{
class MyFunctions
{
public:
static __declspec(dllexport)void HelloFromCPP();

};
}

SimpleDll.cpp:

  #includeSimpleDll.h
#include< stdio.h>
#include< iostream>

using namespace std;

命名空间SimpleDll {
externC{
void MyFunctions :: HelloFromCPP(){
cout< Hello from cpp<< endl;
}
}
}



我还添加了变量


-Djava.library.path =Path\To\ SimpleDll到Intellij运行配置中的VM选项。


但是当我运行程序时,它返回错误:


线程main中的异常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>

解决方案 http://stackoverflow.com/questions/6092200/how-to-fix-an-unsatisfiedlinkerror-cant-find-dependent-libraries-in-a-jni-pro\">如何修复UnsatisfiedLinkError(找不到依赖库)在一个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屋!

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