Simple example of using the Java Native Interface(JNI)
2015-01-23 21:36
501 查看
按以下步骤可以在Java文件中编写native方法.
1. 创建HelloWorld.java文件(declares the native method);
2. 编译"javac HelloWorld.java";
3. 执行"javah -jni HelloWorld"生成HelloWorld.h;
4. 创建HelloWorld.c;
5. 在Redhat5.8中编译HelloWorld.c,生成HelloWorld.so;
6. 设置LD_LIBRARY_PATH;
LD_LIBRARY_PATH=.:${LD_LIBRARY_PATH}; java HelloWorld
或
java -Djava.library.path=. HelloWorld
参考:
http://www.java-tips.org/other-api-tips/jni/simple-example-of-using-the-java-native-interface.html
http://ubuntuforums.org/showthread.php?t=1942105
1. 创建HelloWorld.java文件(declares the native method);
class HelloWorld { static { System.loadLibrary("HelloWorld"); } private native void print(); public static void main(String[] args) { new HelloWorld().print(); } }
2. 编译"javac HelloWorld.java";
3. 执行"javah -jni HelloWorld"生成HelloWorld.h;
4. 创建HelloWorld.c;
#include <jni.h> #include <stdio.h> #include "HelloWorld.h" JNIEXPORT void JNICALL Java_HelloWorld_print(JNIEnv *env, jobject obj) { printf("Hello World!n"); return; }
5. 在Redhat5.8中编译HelloWorld.c,生成HelloWorld.so;
gcc -c -fPIC -I/scratch/luwang/jdk6/include HelloWorld.c gcc -shared -o libHelloWorld.so HelloWorld.o
6. 设置LD_LIBRARY_PATH;
LD_LIBRARY_PATH=.:${LD_LIBRARY_PATH}; java HelloWorld
或
java -Djava.library.path=. HelloWorld
参考:
http://www.java-tips.org/other-api-tips/jni/simple-example-of-using-the-java-native-interface.html
http://ubuntuforums.org/showthread.php?t=1942105
相关文章推荐
- Simple example of using the Java Native Interface
- [论文笔记] Anatomy of a crowdsourcing platform - Using the example of microworkers.com (IMIS, 2011)
- native, The Keyword Of Java
- Java Native Interface Specification(JNI)
- JNI_Java Native Interface
- java native interface(JNI)技术简单应用
- android ---Using java surface on the native side
- [JNI] How to get the java's native method body?
- The Java™ Native Interface
- Java Native Interface Specification(JNI)
- The ins and outs of using Java with Domino
- Using the Java Persistence API with Amazon SimpleDB
- Java Native Interface(JNI)简介
- Get the IP address of a network interface in C using SIOCGIFADDR
- A simple Example of using Video Textures in Managed DirectX (C#)
- 用Delphi开发JNI(Java+Native+Interface)应用
- Java Native Interface Specification contents——jni官方文档
- Using the SAX Interface of LibXML
- Java Programming Tutorial Java Native Interface (JNI)
- Java™ Native Interface (JNI)【读书笔记2】