最近学习了一下win7系统下java调用C语言写的代码,做个总结。
java调用C语言的DLL文件有多种方法
1)JNA—java native方法
2)JNI—java native interface
3)jnative.jar 包
详细参考:
http://blog.csdn.net/ranmudaofa/article/details/9798779
1、我的电脑是win7 64位系统,安装的jdk也是64位,后来始终提示
运行的时候总是报错
JNative library not loaded, sorry !
JNative library not loaded, sorry !
JNative library not loaded, sorry !
JNative library not loaded, sorry !
…
试了各种方法,把
JNativeCpp.dll 放到 C:\Windows\System32
、jdk/bin 目录都特么不行,一直报错,
简直要崩溃了,后来找到一篇文章:
http://blog.csdn.net/longtingjing/article/details/44541649
把64位jdk换成32位jdk 成功了!!!!我擦,真坑啊
2、网上说java调用的C语言文件需要下载
Visual Studio 2010 ,谁特么有功夫下载这么大的编辑器。。。
于是搜索了一些文章,推荐使用MINGW c/c++ 编译工具。
https://sourceforge.net/projects/mingw/
这个是免安装版,下载完之后把路径配置到环境变量里面,我的如下:
Path
C:\ProgramData\Oracle\Java\javapath;C:\Program Files (x86)\RSA SecurID Token Common;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;%SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;%SYSTEMROOT%\System32\WindowsPowerShell\v1.0\;C:\Users\LocalUser\Desktop\adt-bundle-windows-x86-20140702\adt-bundle-windows-x86-20140702\sdk\platform-tools;%JAVA_HOME%\bin;%java_home%\jre\bin;%adb%;C:\Users\lihui20\Downloads\mingw\bin;
注意C:\Users\lihui20\Downloads\mingw\bin;
添加之后可以
使用cmd,在任何路径选使用
强大的 gcc命令。
3、下载JNative 压缩包。
http://sourceforge.NET/projects/jnative/
最好下载所以文件(3个)
Jnative.jar 这个是必须要的,java代码里面会用到。
jnativecpp.dll 后来发现我这个没有其实也可以。网友说最新的Jnative.jar 里面已经包含后jnativecpp.dll 这个文件。经过验证发现确实如此:
lib-bin存在这个文件。不需要再添加了
libjnativecpp.so 这个是linux下使用,也就是android 手机开发app使用的。
3、写c文件。我的例子,一个菜鸟,写的比较简单,java输入一个数组,C语言打印。
array.h :头文件
#ifndef MINGW_DLL_H__
#define MINGW_DLL_H__
void add(int a[5]);
#endif
保存。
再写array.c
#include <stdio.h>
#include "array.h"
void add(int a[5]){
//1.for循环遍历
int i=0;
for (i = 0; i <5; i++)
{
printf("a[%d] = %d\n",i ,a[i]);
}
}
保存
4、使用gcc命令生成 .dll 动态库文件,给java调用
C:>gcc -Wall -shared array.c -o array.dll
注意:上面的array.c array.h文件在一个目录下,我的是在
C:\ 下面。
自动生成array.dll文件。
5、把array.dll放到当前项目的最外层目录下(不放在这也可以,使用绝对路径)。
我的是:E:\ecplicespace\FileExplorer-master\testjni\
如果array.dll文件放的路径不对,会提示:
test
Exception in thread “main” org.xvolks.jnative.exceptions.NativeException: DLL d:\array.dll not found
at org.xvolks.jnative.JNative.nLoadLibrary(Native Method)
at org.xvolks.jnative.JNative.loadLibrary(JNative.java:545)
at org.xvolks.jnative.JNative.(JNative.java:521)
at org.xvolks.jnative.JNative.(JNative.java:440)
at com.study.jnilearn.Test.testJNative(Test.java:14)
at com.study.jnilearn.Test.main(Test.java:50)
java代码:
package com.study.jnilearn;
import org.xvolks.jnative.JNative;
import org.xvolks.jnative.exceptions.NativeException;
import org.xvolks.jnative.pointers.Pointer;
import org.xvolks.jnative.pointers.memory.MemoryBlockFactory;
public class Test {
public static int testJNative(int a[]) throws NativeException,
IllegalAccessException {
JNative n = null;
try {
//这里可以使用绝对路径
n = new JNative("c:\\array.dll", "add");
// n.setRetVal(Type.VOID);
// // n.setParameter(0, a);
// // n.setParameter(1, b);
// n.set
// n.invoke();
// System.out.println("返回:" + n.getRetVal());
// 为数组创建空间
Pointer aArrIntInput = new Pointer(
MemoryBlockFactory.createMemoryBlock(4 * a.length));
// 初始化数组
for (int i = 0; i < a.length; i++) {
aArrIntInput.setIntAt(4 * i, a[i]);
}
// 设定传入参数
n.setParameter(0, aArrIntInput); // 数组指针
// n.setParameter(1, a.length); // 数组大小
// 设定返回类型
// n.setRetVal(Type.VOID);
// 调用DLL
n.invoke();
// return Integer.parseInt(n.getRetVal());
} finally {
if (n != null)
n.dispose();
}
return 0;
}
public static void main(String[] args) throws NativeException,
IllegalAccessException {
System.out.println("test");
testJNative(new int[] { 1, 2, 3, 4, 9 });
// System.loadLibrary("array");
// Value v=new Value();
// int a[]={1,2,3,4};
// int b[]={6,7,8,9};
// v.add(a);
}
}
结果:
test
a[0] = 1
a[1] = 2
a[2] = 3
a[3] = 4
a[4] = 9
这里,win7下 java 使用jnative 包调用 c 的简单demo完成了,
当然更复杂的功能编写实现,还需要我们继续学习。