java使用JNI调用C++函数
网上有了很多这样的文章,不过感觉并不是很全,我都东找西找才最终把JAVA调用c++给搞定。
下面引用了很多网上已经有的的内容,我在这里只是整合一次,让以后的朋友少走网路。
多说无益,具体如下:
作者:mykoma
假设所有文件都放在d:\
Step 1:
写JAVA文件
例如程序testdll.java,内容为:
public class testdll {
static{
System.loadLibrary("testdll");
}
public native static int get();
public native static void set(int i);
public static void main(String[] args) {
testdll test = new testdll();
test.set(10);
System.out.println(test.get());
}
}
用javac testdll.java编译它,会生成testdll.class。
再用javah testdll,则会在当前目录下生成testdll.h文件,这个文件需要被C/C++程序调用来生成所需的库文件。
Step 2:
根据testdll.h写C++文件testdll.cpp
testdll.cpp内容为:
#include "testdll.h"
int i = 0;
JNIEXPORT jint JNICALL Java_testdll_get (JNIEnv *, jclass) {
return 2*i;
}
JNIEXPORT void JNICALL Java_testdll_set (JNIEnv *, jclass, jint j) {
i = j;
}
Step 3:
生成testdll.dll
这一步需要注意,我最开始也是找了很多资料才弄正确
这里我使用的V
相关文档:
http://www.trendcaller.com/2009/05/hadoop-should-target-cllvm-not-java.html
Sunday, May 10, 2009
Hadoop should target C++/LLVM, not Java (because of watts)
< type="text/javascript">
digg_url="http://www.trendcaller.com/2009/05/hadoop-should-target-cllvm-not-java.html";
Over the years, ......
jjhou.csdn.net里面有
--------------------------------------------------------------------------------
在 console mode 中使用 C/C++ 编译器
侯捷 1999.04.08
......
很多网友咨询学习Java有没有什么捷径,我说“无他,唯手熟尔”。但是尚学堂愿意将一些经验写出来,以便后来者少走弯路,帮助别人是最大的快乐嘛!
要想学好Java,首先要知道Java的大致分类。我们知道,自从Sun推出Java以来,就力图使之无所不包,所以Java发展到现在,按应用来分主要分为三大块:J2SE,J2ME和J2EE, ......
- 加入菜鸟学习网,获得珍藏资源
Java代码 数据挖掘中决策树C4.5预测算法实现(半成品,还要写规则后剪枝及对非离散数据信息增益计算) Java代码 package org.struct.decisiontree; import java.util.ArrayList; import java.util.Arrays; ......