讲解java虚拟机以及堆和栈的:http://blog.csdn.net/cutesource/article/details/8244250
编辑helloJNI.java
class helloJNI { public native int get(); public native void set(byte c); static { //System.out.println(System.getProperty("java.library.path")); System.load("/usr/lib/helloJNI.so"); //System.out.println("loading ok"); } public static void main(String[] args) { System.out.println("ing main 1"); helloJNI test = new helloJNI(); System.out.println("ing main "); byte c = (byte)0xff; test.set(c); System.out.println("ing main 3"); System.out.println("test:" + String.valueOf(test.get())); } }
javac helloJNI.java
生成头文件:javah -jni helloJNI
编辑helloJNI.cpp
#include "helloJNI.h" int ret=0; JNIEXPORT jint JNICALL Java_helloJNI_get (JNIEnv *env, jobject jo) { return ret; } JNIEXPORT void JNICALL Java_helloJNI_set (JNIEnv *env, jobject jo, jbyte c) { unsigned char cc = c; ret = cc; }
编译:
gcc -I/usr/local/lib/jdk1.7.0_71/include/linux -I/usr/local/lib/jdk1.7.0_71/include -fPIC -shared -o helloJNI.so helloJNI.c
将 helloJNI.so 复制到/usr/lib中:
sudo cp -v helloJNI.so /usr/lib/
这里执行javah命令时与class文件在同一目录下。而如果class文件放入了包中
如:
package com.test.demo;
class TestHello{
...
}
需要建立相应的目录com/test/demo/TestHello.class
然后退出到com目录的上层目录进行:javah -jni com.test.demo.TestHello
就能够正确的生成TestHello.class的头文件了,(头文件位于当前目录下)。
jar -cvf TestHello.jar com/test/demo/TetsHello.class
------
http://blog.csdn.net/liu251/article/details/3953906
时间: 2024-10-07 15:51:09