您的位置:首页 > 编程语言 > Java开发

使用JNA,让java调用原生代码

2016-01-16 19:48 579 查看

JNA定义:

JNA:java Native Access,是SUN公司开发的基于JNI的框架,JNI使得Java能够调用原生的c或者c++代码。

JNA与JNI(Java Native Interface)的区别:

性能:JNA在性能上不如JNI,由于JNA是在JNI的基础上封装了一层。

移植性:JNA的可移植性要好于JNI,因为开发者不需要再编写作为代理的动态链接库。

使用:JNI使用native关键字,使用一个个java方法映射原生方法,利用System.loadLibrary;JNA使用一个java借口来代表动态链接库,使用Native.loadLibrary

JNA使用环境安装:

原生代码:使用C++或者C编写原生代码,或者使用已有的原生代码,在准备在java中使用的函数或者class前注明extern “C” __declspec(dllexport),然后打包成动态链接库dll

Java代码:下载jna.jar,https://github.com/java-native-access/jna,然后把dll文件放在具体的工程下面。

JNA使用:

1. 准备dll(这部分针对初学者,可直接浏览第2部分)

我们从生成dll到利用JNA,使用Java调用dll一步一步讲解。

首先生成dll,为了学习JNA,最好我们自己动手生成dll,我是使用Dev C++(http://sourceforge.net/projects/orwelldevcpp/) 这个工具来编写c++代码的。安装Dev C++之后,我们创建一个dll工程。




然后我们创建一个.h文件以及一个.cpp文件。我们需要在.h里面define:

[code]#if BUILDING_DLL
#define DLLIMPORT extern "C" __declspec(dllexport)
#else
#define DLLIMPORT extern "C" __declspec(dllimport)
#endif


然后我们在.h定义两个Struct以及两个函数function:

[code]struct UserStruct{
    long id;
    wchar_t* name;
    int age;
};
DLLIMPORT
void sayUser(UserStruct* pUserStruct);
struct CompanyStruct{
    long id;
    wchar_t* name;
    UserStruct* users[100];
    int count;
};
DLLIMPORT
void sayCompany(CompanyStruct* pCompanyStruct);


这里需要注意,函数声明前需要加DLLIMPORT,这里DLLIMPORT等于extern “C” __declspec(dllexport),Struct前不需要做特殊的处理。

在.cpp文件中实现sayUser以及sayCompany两个函数:

[code]void sayUser(UserStruct* pUserStruct)
{
    std::wcout<<L"hello:"<<pUserStruct->name<<std::endl;
}


sayUser函数的功能是把UserStruct结构体中的的name打印出来

[code]void sayCompany(CompanyStruct* pCompanyStruct)
{
    std::wcout<<L"hello:"<<pCompanyStruct->name<<std::endl;
    for(int i=0;i<pCompanyStruct->count;i++)
    {
        UserStruct* user=pCompanyStruct->users[i];
        sayUser(user);
    }
}


SayCompany函数的功能是把Company结构体中的name打印出来,同时把里面所有UserStruct成员的name打印出来。

然后我们在cpp文件中实现

[code]BOOL WINAPI DllMain(HINSTANCE hinstDLL,DWORD fdwReason,LPVOID lpvReserved)
{
    return TRUE;
}


然后编译生成dll文件,由于我定义的工程名字叫做JNATest,所以生成的dll文件叫做JNATest.dll。

2. Java使用JNA调用dll

我们需要创建一个interface,这里我们取名TestDll1,然后extends Library。我们需要把UserStruct和CompanyStruct两个结构体映射到java中,代码如下:

[code]public class UserStruct extends Structure {
        public static class ByReference extends UserStruct implements Structure.ByReference{};
        public static class ByValue extends UserStruct implements Structure.ByValue{}
        public NativeLong id;
        public WString name;
        public int age;
}


其中ByReference指的是指针,ByValue指的是值,然后下面三个字段分别对应.h文件中UserStruct中的三个字段,其中顺序一定不能错,因为作为内存传给c函数调用,读取时按照c中结构体的顺序来读,所以顺序一定不能错。同理CompanyStruct在java中的映射如下:

[code]public class CompanyStruct extends Structure{
        public static class ByReference extends CompanyStruct implements Structure.ByReference{};
        public static class ByValue extends CompanyStruct implements Structure.ByValue{};
        public NativeLong id;
        public WString name;
        //需要使用toArray,因为java中的内存空间是不连续的,所以使用JNA提供的toArray方法生成连续的内存空间
        public UserStruct.ByReference[] users=(UserStruct.ByReference[]) new UserStruct.ByReference().toArray(100);
        public int count;
}


这里面有个需要特别注意的地方,否则会NULL错误,就是public UserStruct.ByReference[] users=(UserStruct.ByReference[]) new UserStruct.ByReference().toArray(100);这句话,如果改为public UserStruct.ByReference[] users=new UserStruct.ByReference[100],之后调用就会报错,原因是java的内存空间一般是不连续的,而我们需要连续的内存空间,这里需要使用JNA提供的toArray函数生成数组。

下面一段代码很重要就是载入dll:

[code]TestDll1 INSTANCE=(TestDll1) Native.loadLibrary("JNATest",TestDll1.class);


然后声明我们要在java中调用的原生函数:

[code]public void sayUser(UserStruct userStruct);
public void sayCompany(CompanyStruct companyStruct);


然后我们创建一个Test.java,然后在main函数中测试是否能调用原生函数。

首先测试sayUser:

[code]UserStruct.ByReference userStruct=new UserStruct.ByReference();
userStruct.id=new NativeLong(100);
userStruct.age=30;
userStruct.name=new WString("SBY");
TestDll1.INSTANCE.sayUser(userStruct);


然后运行,会打印出hello:SBY

再测试sayCompany:

[code]CompanyStruct.ByReference companyStruct=new CompanyStruct.ByReference();
companyStruct.id=new NativeLong(2);
companyStruct.name=new WString("hehe");
companyStruct.count=10;
UserStruct.ByReference pUserStruct=new UserStruct.ByReference();
pUserStruct.id=new NativeLong(90);
pUserStruct.age=99;
pUserStruct.name=new WString("sby");
//pUserStruct.write();
for(int i=0;i<companyStruct.count;i++){
    companyStruct.users[i]=pUserStruct;
}
TestDll1.INSTANCE.sayCompany(companyStruct);


会输出一个hello:hehe和10个hello:sby

有些地方会说需要pUserStruct.write()这行代码,目的是把内存固定住,而不被GC释放掉,然而在我测试的时候没有加这一行也能准确运行,估计这个JNA提供的toArray函数有关。

以上就是我的JNA学习心得,之所以会在machine learning的博客中插入这样一篇,主要的目的是大多数的machine leanring的代码都是使用c或者c++编写,而一些场景会需要编写java程序,此时我们需要使用java来调用已经写好的c或者c++函数。

以上源代码传送门:http://yun.baidu.com/share/link?shareid=2278504517&uk=3977203577
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: