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

Java:vs生成JNI,java调用JNI实例

2013-01-26 11:54 435 查看
了kimmking的JNI技术实践小结,又读了danlley的Java JNI 编程进阶,这些文章中都是自己调用自己,方法很java化,而对于真正的调用dll还是不理解,心中一直存在问题:现在系统中已经有的dll我该如何去调用?如果一个大工程里需要cpp和java一起开发,cpp给出接口、SDK,我该如何处理?

带着这些疑问我决定从cpp到java的jni调用这个全过程亲自动手操作一下。

完成此示例需要下列工具/环境:

1、java环境(废话谁都知道)

2、编译c/cpp的工具。推荐用vs/vc++,我用的是vs2008

一 先制作一个系统中有的DLL文件(cpp给出的sdk接口)

既然是测试我们就把我们这个dll叫做testDll吧,为了简单其间,我只写一个add方法,就是简单的2个数字相加,对于真正的开发中我们肯定会遇到其他类型,java到c/cpp中类型需要转换,具体类型转换对应关系g一下就能得到,我也不在列举。c/cpp中一个class一般包含2个文件,一个头文件定义(*.h),一个文件主体(*.c/*.cpp)。啰嗦了这么多还是直接动手吧,先在vs2008中建立一个工程(当然你也可以直接编写不用这些IDE工具,gcc g++的命令自己g。下同,不在注释不在废话),选取win32工程



键入工程名字testDll,点击next选取DLL,然后点击完成



打开我们的testdll.cpp,添加进我们的add方法

C++代码







int add(int a,int b){

return a+b;
}

int add(int a,int b){
return a+b;
}


注意到文件列表里并没有testDll.h,因为我们要给出调用者一个接口,如果不给头文件,人家就没办法调用,所以我们就必须添加一个头文件testDll.h。

C++代码







#ifdef TEST_DLL
#define TEST_API __declspec(dllexport)

#else
#define TEST_API __declspec(dllimport)

#endif

/* Set up for C function definitions, even when using C++ */
#ifdef __cplusplus
extern "C" {
#endif

TEST_API int add(int,int);

/* Ends C function definitions when using C++ */
#ifdef __cplusplus
}
#endif

#ifdef TEST_DLL
#define TEST_API __declspec(dllexport)
#else
#define TEST_API __declspec(dllimport)
#endif

/* Set up for C function definitions, even when using C++ */
#ifdef __cplusplus
extern "C" {
#endif

TEST_API int add(int,int);

/* Ends C function definitions when using C++ */
#ifdef __cplusplus
}
#endif
在这个头文件中我们把我们的add方法给定义了进去。注意到testdll.cpp中#include "stdafx.h",所以我们就把这个testDll.h include进stdafx.h里面。

按道理说我们的这个dll已经完成了,但是一般c/cpp给接口SDK的时候大都给.h和.lib,为了一步生成dll和lib,我们添加进一个testDll.def,有了这个文件就可以一步生成dll和lib。在source file里右键add new item ,选择Module-Definition File



键入testDll,OK了,我们可以直接build了。生成testDll.dll和testDll.lib。

把testDll.dll扔到system32目录里等待我们高大威猛的java jni调用。

二 JNI

2.1 编写java文件

为了显示我们的与众相同,我们就把我们的这个java文件命名为Demo.java顺便直接带上包名

,因为我们知道人家给我们的接口里有个add方法,所以我们就直接来个调用吧。

Java代码







package com.testJni.testDemo;

public class Demo {
static
{
//System.out.println(System.getProperty("java.library.path"));

System.loadLibrary("testDll");
System.loadLibrary("jniDll");
}
public native static int add(int a,int b);

}

package com.testJni.testDemo;

public class Demo {
static
{
//System.out.println(System.getProperty("java.library.path"));
System.loadLibrary("testDll");
System.loadLibrary("jniDll");
}
public native static int add(int a,int b);

}
demo.java代码暂时如此,我们把将要生成的jni的dll叫做jniDll,有童鞋讲,我不想用你这个烂名字jniDll多俗啊,没关系,你可以换,随你换,生成文件后你再换也可以,现在换也可以。

2.2 生成.h头文件

javah命令,不多讲。生成的文件com_testJni_testDemo_Demo.h这个文件的命名规则我就不多讲了,一目了然。

C++代码







/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class com_testJni_testDemo_Demo */

#ifndef _Included_com_testJni_testDemo_Demo

#define _Included_com_testJni_testDemo_Demo

#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: com_testJni_testDemo_Demo
* Method: add
* Signature: (II)I
*/
JNIEXPORT jint JNICALL Java_com_testJni_testDemo_Demo_add
(JNIEnv *, jclass, jint, jint);

#ifdef __cplusplus
}
#endif
#endif

/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class com_testJni_testDemo_Demo */

#ifndef _Included_com_testJni_testDemo_Demo
#define _Included_com_testJni_testDemo_Demo
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class:     com_testJni_testDemo_Demo
* Method:    add
* Signature: (II)I
*/
JNIEXPORT jint JNICALL Java_com_testJni_testDemo_Demo_add
(JNIEnv *, jclass, jint, jint);

#ifdef __cplusplus
}
#endif
#endif


2.3 用c/cpp实现这个头文件

c/cpp中已经实现了这个add方法,我们只需要调用就可以啦。所以直接vs2008中建立一个dll工程,工程名我们就叫jniDll,具体过程不再多讲,方法同上面testDll的建立一样。在这个工程里kimmking把需要引用的包、文件等已经讲的很清楚了。打开jniDll.cpp,添加下面代码

C++代码







JNIEXPORT jint JNICALL Java_com_testJni_testDemo_Demo_add (JNIEnv *env,jclass jobject,jint a,jint b){ return add(a,b); }

JNIEXPORT jint JNICALL Java_com_testJni_testDemo_Demo_add
(JNIEnv *env,jclass jobject,jint a,jint b){

return add(a,b);
}
因为int对应的类型就刚好是jint,所以就不需要转换,其他需要转换的类型自己g对应关系转换,注意释放。

这个工程里我们还需要打开 stdafx.h添加

C++代码







#include <jni.h> #include "testDll.h" #include "com_testJni_testDemo_Demo.h"

#include <jni.h>

#include "testDll.h"
#include "com_testJni_testDemo_Demo.h"


在编译这个jniDll工程的时候需要引入testDll.h,com_testJni_testDemo_Demo.h,另外添加testDll.lib这个依赖。



好了做好这些后,build下,生成了我们期待已久的jniDll.dll,把这个dll同样扔到system32下。

三 测试

本人特懒,不想写多余的class,所以直接修改Demo.java 这也是刚才为什么讲暂时如此的原因

Java代码







package com.testJni.testDemo;

public class Demo {
static
{
//System.out.println(System.getProperty("java.library.path"));

System.loadLibrary("testDll");
System.loadLibrary("jniDll");
}
public native static int add(int a,int b);

public static void main(String[] args) {
System.out.println(add(7,2));
}
}

package com.testJni.testDemo;

public class Demo {
static
{
//System.out.println(System.getProperty("java.library.path"));
System.loadLibrary("testDll");
System.loadLibrary("jniDll");
}
public native static int add(int a,int b);
public static void main(String[] args) {
System.out.println(add(7,2));
}
}


四 最后补充

如果系统已经加载过c/cpp的dll,我们就不用再System.loadLibrary("testDll")了,加载一遍就可以了,因为我们刚才写的testDll系统没有加载,所以我就加载了一下。对于多个dll可以写多个System.loadLibrary去加载,修改static{}里面的内容不需要重新生成dll,除非你多加了一个调用方法,如果你看清楚规则,就不用javah命令就可以直接编写头文件,用javah太麻烦了。

【参考文章】

http://www.iteye.com/topic/304594 JNI技术实践小结--原理分析和详细步骤截图说明

http://www.iteye.com/topic/295776?page=1 Java JNI 编程进阶
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: