Under Win10 configuration JNI, Java and C ++ bridge of communication

Java JNI is a tool used to interact with the language like C ++, very easy to use, difficult to configure. Because sometimes some of the functions is to use c ++ library written in Java version did not, so use a lot of trouble, but if you can call Java or C ++ dll's lib, it will be very convenient, JNI is one such tool. The following describes how to configure user-friendly:

Java code -

First, write a C ++ function needs to be called in Java, the new class, the keyword is native, as shown in

GetJsonDwg function is implemented in C ++, java here needs to be invoked, a transfer parameter of type String, returns a result of type String. Further, the path need some reference, FIG.

TestJNI is the name of the dll or lib, that is C ++ project name.

When I used to write, as

In the name of the Java class TestJNI here represented (note not a thing of TestJNI indicated above), I happened to be set to the same. hw.GetJsonDwg () method is the application of C ++. This, Java part of the code is complete.

cmd operation -

Next you need to compile the .java file into C ++ .h file that can be referenced, we can easily be done in cmd. First switch cmd into the bin directory of the Java project, directory (using the cd command) that is located .h, as

And enter the following as:

What this means is to compile the .h file generated in the current path, the first file name is com_testjni_TestJNI, not just from the name, com_testjni is where the Java class package name, TestJNI is the name of the library in C ++.

After the implementation of this, in the bin directory will generate a com_testjni_TestJNI.h header file, which is the result of the implementation of the above command line.

C ++ part -

Turning next to the C ++ part, we have created a Win32 console application, the configuration shown in Figure

We project to set properties, C / C ++ - General - Additional Include Directories, add the path jni.h under the Java directory and jni_md.h where, under the path shown in My Computer

General position is basically the same, anyway, as long as the Java directory to find the back portion are looking at include inside.

然后将之前编译的头文件com_testjni_TestJNI.h放到C++的工程目录下,就是放到和.cpp这些文件的同一个目录中,然后右键头文件--添加--现有项,将这个头文件引用进来。如果头文件有问题,就把#include  <jni.h>改为#include "jni.h",反正我是不用改就可以用。

接下来创建一个.cpp,然后在里面写刚刚那个需要的函数,就是GetJsonDwg(),写法如下:

JNIEXPORT jstring JNICALL Java_com_testjni_TestJNI_GetJsonDwg
(JNIEnv *env, jobject obj, jstring filepath){
jstring file = filepath;
jstring a = env->NewStringUTF("LiYu");
return a;
}

红色部分jstring对应Java中的String,绿色部分Java_com_testjni_TestJNI_GetJsonDwg的命名规则是:Java_工程名_函数名,黄色部分是默认的参数,自带,后面的jstring filepath就是传递的参数。以上仅是简单示例,重在告知原理,具体函数内容根据需求来实现。另外这个函数的写法可以到.h文件中去复制就行了!只不过自己要把出传递的参数添加一个具体的名称。

写好后右键项目--生成,然后在工程目录Debug中,我们就可以找到生成的.dll和.lib了。

配置Java Build Path——

右键包--Build Path--Configure Build Path,点击Libraries--JRE System Library--Native library location,点击Edit,把上面的.dll和.lib所在的路径添加进去,Apply and Close。如图

这个步骤是将库引用进来,这样代码中的System.loadLibrary("TestJNI");才能找到对应的库。

这样配置就完成了。

最后运行main函数,应该是可以正常调用C++的库中的函数了,如图

总结——

总结来说,JNI应该是一个不错的原生的接口,可以解决部分库缺乏Java版本的问题。不同的语言之间的交互确实是一个问题,这也包括传输的编码问题。这篇的文章没有深究传输编码问题,实际上如果C++直接返回一个中文字符串时会存在乱码的问题的,因为C++里是gbk编码,需要先转化为utf8/16才行,附代码如下:

//传递中文字符串,gb2312转utf8/16
jstring charTojstring(JNIEnv* env, char* str)
{
	jstring rtn = 0;
	int slen = strlen(str);
	unsigned short * buffer = 0;
	if (slen == 0)
		rtn = (env)->NewStringUTF(str);
	else
	{
		int length = MultiByteToWideChar(CP_ACP, 0, (LPCSTR)str, slen, NULL, 0);
		buffer = (unsigned short *)malloc(length * 2 + 1);
		if (MultiByteToWideChar(CP_ACP, 0, (LPCSTR)str, slen, (LPWSTR)buffer, length) > 0)
			rtn = (env)->NewString((jchar*)buffer, length);
	}
	if (buffer)
		free(buffer);
	return rtn;
}
发布了40 篇原创文章 · 获赞 28 · 访问量 6万+

Guess you like

Origin blog.csdn.net/lyandgh/article/details/80902956