A simple JNI example
来源:互联网 发布:js获取url参数 编辑:程序博客网 时间:2024/06/04 20:07
tested on CentOS 5.8
Step 1: declare a native function in the java source file
public class AddMe{ static { System.loadLibrary("hello"); } public native void sum(int a, int b); public static void main(String[] args){ AddMe am = new AddMe(); am.sum(2, 5); }}
Step 2: compile the java class
javac AddMe.java
Step 3: generate the c header file
javah -jni AddMe
You will find the "AddMe.h" file in the current working directory
Step 4: implement the c function "sum" (by JNI naming convention the function name would be Java_AddMe_sum)
We create a c source file called "addme.c"
#include "AddMe.h"#include <stdio.h>JNIEXPORT void JNICALL Java_AddMe_sum (JNIEnv *env, jobject obj, jint a, jint b){ jint result = 0; result = a + b; printf("hello world %d\n", result);}
Step 5: compile the c code and generate a share library "libhello.so" (it must have the name libhello so that System.loadLibrary("hello") can dynamically load the library)
gcc -fPIC -I/usr/lib/jvm/java/include -o libhello.so -shared ./addme.c
Step 6: run the java bytecode
java -Djava.library.path=. AddMe
- A simple JNI example
- a simple ajax example
- A simple wxPython example
- A simple DirectShow example
- A simple GetOpenFileName example
- A simple makefile example
- a simple example rabbitMQ
- coding with a simple example
- A Simple Quartz/Spring Example
- A Simple Session Establishment Example
- VPython - example - A simple Clock
- A very simple CTransInPlaceFilter example
- A simple libpng example program
- SIP:A Simple Session Establishment Example
- a simple example of Ado.net
- A simple example for test case--triangle
- [旧文] A simple makefile example
- VPython - example - A simple ball acceleration
- hash有关
- ubuntu下ftp服务器的配置
- vs2010中c++编译运行后屏幕一闪而过解决方法(控制台)
- 别指望靠工资发财
- 编程练习2
- A simple JNI example
- gcc: collect2 cannot find 'ld'
- java map 遍历的三种简单的办法
- 在Eclipse中查看Android SDK源码
- Qt容器类介绍和使用
- Javascript函数返回值的一个问题(显式返回和非显式返回值的问题)
- assert只如初见
- Project Euler problem 19
- Eclipse安装git插件EGit