JNI使用示例

转载于http://www.ibm.com/developerworks/java/tutorials/j-jni/j-jni.html#step5

JNI lets Java code use code and code libraries written in other languages, such as C and C++. The Invocation API, which is part of JNI, can be used to embed a Java virtual machine (JVM) into native applications, thereby allowing programmers to call Java code from within native code.

This tutorial deals with the two most common applications of JNI: calling C/C++ code from Java programs, and calling Java code from C/C++ programs. We’ll cover both the essentials of the Java Native Interface and some of the more advanced programming challenges that can arise.

需要的工具和组件

  • A Java compiler: javac.exe ships with the SDK.
  • A Java virtual machine (JVM): java.exe ships with the SDK.
  • A native method C file generator: javah.exe ships with the SDK.
  • Library files and native header files that define JNI. The jni.h C header file, jvm.lib, and jvm.dll or jvm.so files all ship with the SDK.
  • A C and C++ compiler that can create a shared library. The two most common C compilers are Visual C++ for Windows and cc for UNIX-based systems.
  • 即需要安装Java development kit
  • C或者C++的编译器,本实验使用jdk1.8 ubuntu14.04操作系统 GCC编译器。

Calling C/C++ code from Java programs(从Java程序中调用C/C++代码)