课程大纲: 2、windows下java JNI编程技巧——JAVA调用c/c++(0) 3、windows下java JNI编程技巧——JAVA调用c/c++(1) 4、windows下java JNI编程技巧——JAVA调用c/c++(2) 5、windows下java JNI编程技巧——JAVA调用c/c++(3) 6、windows下java JNI编程技巧——JAVA调用c/c++(4)
【教程一】JAVA JNI简介: JNI允许Java代码使用以其它语言(譬如 C 和 C++)编写的代码和代码库。 Invocation API(JNI的一部分)可以用来将Java虚拟机(JVM)嵌入到本机应用程序中,从而允许程序员从本机代码内部调用Java 代码。 也许不少人觉得Java已经足够强大,为什么要需要JNI这种东西呢? 我们知道Java是一种平台无关性的语言,平台对于上层的java代码来说是透明的,所以在多数时间我们是不需要JNI的,但是假如你遇到了如下的三种情况之一呢? 1.你的Java代码,需要得到一个文件的属性。但是你找遍了JDK帮助文档也找不到相关的API。 2.在本地还有一个别的系统,不过他不是Java语言实现的,这个时候你的老板要求你把两套系统整合到一起。 3.你的Java代码中需要用到某种算法,不过算法是用C实现并封装在动态链接库文件(DLL)当中的。 二、环境需求 JNI 最常见的两个应用:从Java程序调用C/C++,以及从C/C++程序调用Java代码 1、需要下列工具与组件: java.exe: Java 虚拟机(JVM):随 SDK 一起提供的 。 javah.exe: 本机方法 C 文件生成器:随 SDK 一起提供的 。 2、定义JNI的库文件和本机头文件: 3、能够创建共享库的 C 和 C++ 编译器。 最常见的两个 C 编译器是用于Windows的Visual C++和用于基于UNIX系统的 gcc/cc。 因此,后面我们将会介绍在两种环境下的JNI编程例子。
【教程二】windows下java JNI编程技巧——JAVA调用c/c++(0) 一、使用情况 当无法用Java语言编写整个应用程序时,JNI允许您使用本机代码。 在下列典型情况下,您可能决定使用本机代码: 1、希望用更低级、更快的编程语言去实现对时间有严格要求的代码。 2、希望从 Java 程序访问旧代码或代码库。 3、需要标准 Java 类库中不支持的依赖于平台的特性。 二、所需软件 eclipse 三、步骤分析 从 Java 程序调用 C 或 C ++ 代码的过程由六个步骤组成: 我们将在下面几页中深入讨论每个步骤,但还是先让我们迅速地浏览一下它们: 1、编写 Java 代码。 我们将从编写 Java 类开始,这些类执行三个任务: 1)声明将要调用的本机方法; 2)装入包含本机代码的共享库; 3)然后调用该本机方法。 2、编译 Java 代码。 在使用 Java 类之前,必须成功地将它们编译成字节码。 3、创建C/C++头文件。 然后,这个头文件与 C/C++ 函数实现(请参阅步骤 4)一起来创建共享库(请参阅步骤 5)。 4、编写 C/C++ 代码。 C/C++ 源文件必须包含步骤 3 中创建的头文件。 5、创建共享库文件。 从步骤 4 中创建的C源代码文件来创建共享库文件。 6、运行 Java 程序。 运行该代码,并查看它是否有用。我们还将讨论一些用于解决常见错误的技巧。
【教程三】windows下java JNI编程技巧——JAVA调用c/c++(1) 步骤 1:编写 Java 代码 这里是名为JNI_javaCallc_test: 直接使用文本编辑器或在ecilpos中建立工程敲入以下代码: [cpp] view plaincopy <!--[if !supportLists]-->1. <!--[endif]-->package test; <!--[if !supportLists]-->2. <!--[endif]--> <!--[if !supportLists]-->3. <!--[endif]-->public class JNI_javaCallc_test { <!--[if !supportLists]-->4. <!--[endif]--> <!--[if !supportLists]-->5. <!--[endif]--> //c/c++本地方法 <!--[if !supportLists]-->6. <!--[endif]--> public native int intMethod(int n); <!--[if !supportLists]-->7. <!--[endif]--> <!--[if !supportLists]-->8. <!--[endif]--> public native boolean booleanMethod(boolean bool); <!--[if !supportLists]-->9. <!--[endif]--> <!--[if !supportLists]-->10. <!--[endif]--> public native String stringMethod(String text); <!--[if !supportLists]-->11. <!--[endif]--> <!--[if !supportLists]-->12. <!--[endif]--> public native int intArrayMethod(int[] intArray); <!--[if !supportLists]-->13. <!--[endif]--> <!--[if !supportLists]-->14. <!--[endif]--> //java main方法 <!--[if !supportLists]-->15. <!--[endif]--> public static void main(String[] args){ <!--[if !supportLists]-->16. <!--[endif]--> //包含c语言动态库 <!--[if !supportLists]-->17. <!--[endif]--> System.loadLibrary("test_JNI_javaCallc_test"); <!--[if !supportLists]-->18. <!--[endif]--> <!--[if !supportLists]-->19. <!--[endif]--> JNI_javaCallc_test sample = new JNI_javaCallc_test(); <!--[if !supportLists]-->20. <!--[endif]--> <!--[if !supportLists]-->21. <!--[endif]--> int square = sample.intMethod(5); <!--[if !supportLists]-->22. <!--[endif]--> <!--[if !supportLists]-->23. <!--[endif]--> boolean bool = sample.booleanMethod(true); <!--[if !supportLists]-->24. <!--[endif]--> <!--[if !supportLists]-->25. <!--[endif]--> String text =sample.stringMethod("JAVA"); <!--[if !supportLists]-->26. <!--[endif]--> <!--[if !supportLists]-->27. <!--[endif]--> int sum = sample.intArrayMethod(new int[] { 1, 1, 2, 3, 5, 8, 13 }); <!--[if !supportLists]-->28. <!--[endif]--> <!--[if !supportLists]-->29. <!--[endif]--> System.out.println("intMethod: " + square); <!--[if !supportLists]-->30. <!--[endif]--> <!--[if !supportLists]-->31. <!--[endif]--> System.out.println("booleanMethod: " + bool); <!--[if !supportLists]-->32. <!--[endif]--> <!--[if !supportLists]-->33. <!--[endif]--> System.out.println("stringMethod: " + text); <!--[if !supportLists]-->34. <!--[endif]--> <!--[if !supportLists]-->35. <!--[endif]--> System.out.println("intArrayMethod: " + sum); <!--[if !supportLists]-->36. <!--[endif]--> } <!--[if !supportLists]-->37. <!--[endif]-->} 这段代码做了些什么? native 关键字告诉 Java 编译器:方法是用 Java 类之外的本机代码实现的,但其声明却在 Java 中。只能在 Java 类中声明本机方法,而不能实现它(但是不能声明为抽象的方法,使用native关键字即可),所以本机方法不能拥有方法主体。
【教程四】windows下java JNI编程技巧——JAVA调用c/c++(2) 步骤 2:编译 Java 代码 接下来,我们需要将 Java 代码编译成字节码。 完成这一步的方法之一是使用随SDK一起提供的Java编译器javac。 用来将 Java 代码编译成字节码的命令是: cd test 如果是在eclipse环境下编写的以上代码,文件保存时会自动在工程目录的bin下生成以上java文件 步骤 3:创建 C/C++ 头文件 第三步是创建 C/C++ 头文件,它定义本机函数说明。 完成这一步的方法之一是使用 javah.exe,它是随 SDK 一起提供的本机方法 C 存根生成器工具。 这个工具被设计成用来创建头文件,该头文件为在 Java 源代码文件中所找到的每个 native 方法定义 C 风格的函数。 这里使用的命令是: 注意.和test之间有空格 会生成以下文件: [cpp] view plaincopy <!--[if !supportLists]-->1. <!--[endif]--><span style="font-family:SimSun;font-size:16px;">/* DO NOT EDIT THIS FILE - it is machine generated */ <!--[if !supportLists]-->2. <!--[endif]-->#include <jni.h> <!--[if !supportLists]-->3. <!--[endif]-->/* Header for class test_JNI_javaCallc_test */ <!--[if !supportLists]-->4. <!--[endif]--> <!--[if !supportLists]-->5. <!--[endif]-->#ifndef _Included_test_JNI_javaCallc_test <!--[if !supportLists]-->6. <!--[endif]-->#define _Included_test_JNI_javaCallc_test <!--[if !supportLists]-->7. <!--[endif]-->#ifdef __cplusplus <!--[if !supportLists]-->8. <!--[endif]-->extern "C" { <!--[if !supportLists]-->9. <!--[endif]-->#endif <!--[if !supportLists]-->10. <!--[endif]-->/* <!--[if !supportLists]-->11. <!--[endif]--> * Class: test_JNI_javaCallc_test <!--[if !supportLists]-->12. <!--[endif]--> * Method: intMethod <!--[if !supportLists]-->13. <!--[endif]--> * Signature: (I)I <!--[if !supportLists]-->14. <!--[endif]--> */ <!--[if !supportLists]-->15. <!--[endif]-->JNIEXPORT jint JNICALL Java_test_JNI_1javaCallc_1test_intMethod <!--[if !supportLists]-->16. <!--[endif]--> (JNIEnv *, jobject, jint); <!--[if !supportLists]-->17. <!--[endif]--> <!--[if !supportLists]-->18. <!--[endif]-->/* <!--[if !supportLists]-->19. <!--[endif]--> * Class: test_JNI_javaCallc_test <!--[if !supportLists]-->20. <!--[endif]--> * Method: booleanMethod <!--[if !supportLists]-->21. <!--[endif]--> * Signature: (Z)Z <!--[if !supportLists]-->22. <!--[endif]--> */ <!--[if !supportLists]-->23. <!--[endif]-->JNIEXPORT jboolean JNICALL Java_test_JNI_1javaCallc_1test_booleanMethod <!--[if !supportLists]-->24. <!--[endif]--> (JNIEnv *, jobject, jboolean); <!--[if !supportLists]-->25. <!--[endif]--> <!--[if !supportLists]-->26. <!--[endif]-->/* <!--[if !supportLists]-->27. <!--[endif]--> * Class: test_JNI_javaCallc_test <!--[if !supportLists]-->28. <!--[endif]--> * Method: stringMethod <!--[if !supportLists]-->29. <!--[endif]--> * Signature: (Ljava/lang/String;)Ljava/lang/String; <!--[if !supportLists]-->30. <!--[endif]--> */ <!--[if !supportLists]-->31. <!--[endif]-->JNIEXPORT jstring JNICALL Java_test_JNI_1javaCallc_1test_stringMethod <!--[if !supportLists]-->32. <!--[endif]--> (JNIEnv *, jobject, jstring); <!--[if !supportLists]-->33. <!--[endif]--> <!--[if !supportLists]-->34. <!--[endif]-->/* <!--[if !supportLists]-->35. <!--[endif]--> * Class: test_JNI_javaCallc_test <!--[if !supportLists]-->36. <!--[endif]--> * Method: intArrayMethod <!--[if !supportLists]-->37. <!--[endif]--> * Signature: ([I)I <!--[if !supportLists]-->38. <!--[endif]--> */ <!--[if !supportLists]-->39. <!--[endif]-->JNIEXPORT jint JNICALL Java_test_JNI_1javaCallc_1test_intArrayMethod <!--[if !supportLists]-->40. <!--[endif]--> (JNIEnv *, jobject, jintArray); <!--[if !supportLists]-->41. <!--[endif]--> <!--[if !supportLists]-->42. <!--[endif]-->#ifdef __cplusplus <!--[if !supportLists]-->43. <!--[endif]-->} <!--[if !supportLists]-->44. <!--[endif]-->#endif <!--[if !supportLists]-->45. <!--[endif]-->#endif<strong> <!--[if !supportLists]-->46. <!--[endif]--></strong></span>
如您可能已经注意到的那样,JNI_javaCallc_test.h 中的 C/C++ 函数说明和 JNI_javaCallc_test.java 中的 Java native 方法声明有很大差异。 1、JNIEXPORT 和 JNICALL 是用于导出函数的、依赖于编译器的指示符。 2、返回类型、参数类型是映射到 Java 类型的 C/C++ 类型,比如:jstring,jint 现在来介绍下JNI里的数据类型: 在C++里,编译器会很据所处的平台来为一些基本的数据类型来分配长度,因此也就造成了平台不一致性,而这个问题在Java中则不存在,因为有JVM的缘故,所以Java中的基本数据类型在所有平台下得到的都是相同的长度,比如int的宽度永远都是32位。基于这方面的原因,java和c++的基本数据类型就需要实现一些mapping,保持一致性。 下面的表可以概括:下标列举了常见的c/c++到到java的类型映射表。
除了 Java 声明中的一般参数以外,所有这些函数的参数表中都有一个指向 JNIEnv 和 jobject 的指针。 指向 JNIEnv 的指针实际上是一个指向函数指针表的指针。 正如将要在步骤4 中看到的,这些函数提供各种用来在C和C++中操作Java数据的能力。 jobject 参数引用当前对象 因此,如果C或C++代码需要引用Java函数,则这个jobject充当引用或指针,返回调用的 Java 对象。 函数名本身是由前缀“Java_”加全限定类名,再加下划线和方法名构成的。
【教程五】windows下java JNI编程技巧——JAVA调用c/c++(3)
步骤 4:编写 C/C++ 代码 当谈到编写 C/C++ 函数实现时,有一点需要牢记:说明必须和 JNI_javaCallc_test.h 的函数声明完全一样。 我们将研究用于 C 实现和 C++ 实现的完整代码,然后讨论两者之间的差异。 C函数实现 以下是 JNI_javaCallc_test.c,它是用 C 编写的实现: [cpp] view plaincopy <!--[if !supportLists]-->1. <!--[endif]-->#include <jni.h> <!--[if !supportLists]-->2. <!--[endif]-->/* Header for class test_JNI_javaCallc_test */ <!--[if !supportLists]-->3. <!--[endif]--> <!--[if !supportLists]-->4. <!--[endif]--> <!--[if !supportLists]-->5. <!--[endif]-->/* <!--[if !supportLists]-->6. <!--[endif]--> * Class: test_JNI_javaCallc_test <!--[if !supportLists]-->7. <!--[endif]--> * Method: intMethod <!--[if !supportLists]-->8. <!--[endif]--> * Signature: (I)I <!--[if !supportLists]-->9. <!--[endif]--> */ <!--[if !supportLists]-->10. <!--[endif]-->JNIEXPORT jint JNICALL Java_test_JNI_1javaCallc_1test_intMethod(JNIEnv *env, jobject obj, jint num) <!--[if !supportLists]-->11. <!--[endif]--> { <!--[if !supportLists]-->12. <!--[endif]--> return num * num; <!--[if !supportLists]-->13. <!--[endif]--> } <!--[if !supportLists]-->14. <!--[endif]--> <!--[if !supportLists]-->15. <!--[endif]-->/* <!--[if !supportLists]-->16. <!--[endif]--> * Class: test_JNI_javaCallc_test <!--[if !supportLists]-->17. <!--[endif]--> * Method: booleanMethod <!--[if !supportLists]-->18. <!--[endif]--> * Signature: (Z)Z <!--[if !supportLists]-->19. <!--[endif]--> */ <!--[if !supportLists]-->20. <!--[endif]-->JNIEXPORT jboolean JNICALL Java_test_JNI_1javaCallc_1test_booleanMethod <!--[if !supportLists]-->21. <!--[endif]--> (JNIEnv *env, jobject obj, jboolean boolean) { <!--[if !supportLists]-->22. <!--[endif]--> return!boolean; <!--[if !supportLists]-->23. <!--[endif]-->} <!--[if !supportLists]-->24. <!--[endif]-->/* <!--[if !supportLists]-->25. <!--[endif]--> * Class: test_JNI_javaCallc_test <!--[if !supportLists]-->26. <!--[endif]--> * Method: stringMethod <!--[if !supportLists]-->27. <!--[endif]--> * Signature: (Ljava/lang/String;)Ljava/lang/String; <!--[if !supportLists]-->28. <!--[endif]--> */ <!--[if !supportLists]-->29. <!--[endif]-->JNIEXPORT jstring JNICALL Java_test_JNI_1javaCallc_1test_stringMethod <!--[if !supportLists]-->30. <!--[endif]--> (JNIEnv *env, jobject obj, jstring string) <!--[if !supportLists]-->31. <!--[endif]--> { <!--[if !supportLists]-->32. <!--[endif]--> const char *str = (*env)->GetStringUTFChars(env, string, 0); <!--[if !supportLists]-->33. <!--[endif]--> char cap[128]; <!--[if !supportLists]-->34. <!--[endif]--> strcpy(cap, str); <!--[if !supportLists]-->35. <!--[endif]--> (*env)->ReleaseStringUTFChars(env, string, str); <!--[if !supportLists]-->36. <!--[endif]--> return (*env)->NewStringUTF(env, strupr(cap)); <!--[if !supportLists]-->37. <!--[endif]-->} <!--[if !supportLists]-->38. <!--[endif]--> <!--[if !supportLists]-->39. <!--[endif]-->/* <!--[if !supportLists]-->40. <!--[endif]--> * Class: test_JNI_javaCallc_test <!--[if !supportLists]-->41. <!--[endif]--> * Method: intArrayMethod <!--[if !supportLists]-->42. <!--[endif]--> * Signature: ([I)I <!--[if !supportLists]-->43. <!--[endif]--> */ <!--[if !supportLists]-->44. <!--[endif]-->JNIEXPORT jint JNICALL Java_test_JNI_1javaCallc_1test_intArrayMethod <!--[if !supportLists]-->45. <!--[endif]--> (JNIEnv *env, jobject obj, jintArray array) <!--[if !supportLists]-->46. <!--[endif]--> { <!--[if !supportLists]-->47. <!--[endif]--> int i, sum = 0; <!--[if !supportLists]-->48. <!--[endif]--> jsize len = (*env)->GetArrayLength(env, array); <!--[if !supportLists]-->49. <!--[endif]--> jint*body = (*env)->GetIntArrayElements(env, array, 0); <!--[if !supportLists]-->50. <!--[endif]--> for(i=0; i<len; i++) <!--[if !supportLists]-->51. <!--[endif]--> { sum += body[i]; <!--[if !supportLists]-->52. <!--[endif]--> } <!--[if !supportLists]-->53. <!--[endif]--> (*env)->ReleaseIntArrayElements(env, array, body, 0); <!--[if !supportLists]-->54. <!--[endif]--> return sum; <!--[if !supportLists]-->55. <!--[endif]-->}
C++ 函数实现 [cpp] view plaincopy <!--[if !supportLists]-->1. <!--[endif]-->#include <jni.h> <!--[if !supportLists]-->2. <!--[endif]-->/* Header for class test_JNI_javaCallc_test */ <!--[if !supportLists]-->3. <!--[endif]--> <!--[if !supportLists]-->4. <!--[endif]--> <!--[if !supportLists]-->5. <!--[endif]-->/* <!--[if !supportLists]-->6. <!--[endif]--> * Class: test_JNI_javaCallc_test <!--[if !supportLists]-->7. <!--[endif]--> * Method: intMethod <!--[if !supportLists]-->8. <!--[endif]--> * Signature: (I)I <!--[if !supportLists]-->9. <!--[endif]--> */ <!--[if !supportLists]-->10. <!--[endif]-->JNIEXPORT jint JNICALL Java_test_JNI_1javaCallc_1test_intMethod(JNIEnv *env, jobject obj, jint num) <!--[if !supportLists]-->11. <!--[endif]--> { <!--[if !supportLists]-->12. <!--[endif]--> return num * num; <!--[if !supportLists]-->13. <!--[endif]--> } <!--[if !supportLists]-->14. <!--[endif]--> <!--[if !supportLists]-->15. <!--[endif]-->/* <!--[if !supportLists]-->16. <!--[endif]--> * Class: test_JNI_javaCallc_test <!--[if !supportLists]-->17. <!--[endif]--> * Method: booleanMethod <!--[if !supportLists]-->18. <!--[endif]--> * Signature: (Z)Z <!--[if !supportLists]-->19. <!--[endif]--> */ <!--[if !supportLists]-->20. <!--[endif]-->JNIEXPORT jboolean JNICALL Java_test_JNI_1javaCallc_1test_booleanMethod <!--[if !supportLists]-->21. <!--[endif]--> (JNIEnv *env, jobject obj, jboolean boolean) { <!--[if !supportLists]-->22. <!--[endif]--> return!boolean; <!--[if !supportLists]-->23. <!--[endif]-->} <!--[if !supportLists]-->24. <!--[endif]-->/* <!--[if !supportLists]-->25. <!--[endif]--> * Class: test_JNI_javaCallc_test <!--[if !supportLists]-->26. <!--[endif]--> * Method: stringMethod <!--[if !supportLists]-->27. <!--[endif]--> * Signature: (Ljava/lang/String;)Ljava/lang/String; <!--[if !supportLists]-->28. <!--[endif]--> */ <!--[if !supportLists]-->29. <!--[endif]-->JNIEXPORT jstring JNICALL Java_test_JNI_1javaCallc_1test_stringMethod <!--[if !supportLists]-->30. <!--[endif]--> (JNIEnv *env, jobject obj, jstring string) <!--[if !supportLists]-->31. <!--[endif]--> { <!--[if !supportLists]-->32. <!--[endif]--> constchar *str = env->GetStringUTFChars(string, 0); <!--[if !supportLists]-->33. <!--[endif]--> char cap[128]; <!--[if !supportLists]-->34. <!--[endif]--> strcpy(cap, str); <!--[if !supportLists]-->35. <!--[endif]--> env->ReleaseStringUTFChars(string, str); <!--[if !supportLists]-->36. <!--[endif]--> returnenv->NewStringUTF(strupr(cap)); <!--[if !supportLists]-->37. <!--[endif]-->} <!--[if !supportLists]-->38. <!--[endif]--> <!--[if !supportLists]-->39. <!--[endif]-->/* <!--[if !supportLists]-->40. <!--[endif]--> * Class: test_JNI_javaCallc_test <!--[if !supportLists]-->41. <!--[endif]--> * Method: intArrayMethod <!--[if !supportLists]-->42. <!--[endif]--> * Signature: ([I)I <!--[if !supportLists]-->43. <!--[endif]--> */ <!--[if !supportLists]-->44. <!--[endif]-->JNIEXPORT jint JNICALL Java_test_JNI_1javaCallc_1test_intArrayMethod <!--[if !supportLists]-->45. <!--[endif]--> (JNIEnv *env, jobject obj, jintArray array) <!--[if !supportLists]-->46. <!--[endif]--> { <!--[if !supportLists]-->47. <!--[endif]--> int i,sum = 0; <!--[if !supportLists]-->48. <!--[endif]--> jsizelen = env->GetArrayLength(array); <!--[if !supportLists]-->49. <!--[endif]--> jint*body = env->GetIntArrayElements(array, 0); <!--[if !supportLists]-->50. <!--[endif]--> for(i=0; i<len; i++) <!--[if !supportLists]-->51. <!--[endif]--> { <!--[if !supportLists]-->52. <!--[endif]--> sum += body[i]; <!--[if !supportLists]-->53. <!--[endif]--> } <!--[if !supportLists]-->54. <!--[endif]--> env->ReleaseIntArrayElements(array, body, 0); <!--[if !supportLists]-->55. <!--[endif]--> returnsum; <!--[if !supportLists]-->56. <!--[endif]-->}
C 和 C++ 函数实现的比较 在C中,JNI 函数调用由“(*env)->”作前缀,目的是为了取出函数指针所引用的值。 在 C++ 中,JNIEnv 类拥有处理函数指针查找的内联成员函数。 C语法: jsize len = (*env)->GetArrayLength(env,array); C++语法: jsize len =env->GetArrayLength(array);
【教程六】windows下java JNI编程技巧——JAVA调用c/c++(4) 步骤 5:创建共享库文件 接下来,我们创建包含本机代码的共享库文件。 大多数 C 和 C++ 编译器除了可以创建机器代码可执行文件以外,也可以创建共享库文件。 用来创建共享库文件的命令取决于您使用的编译器。 下面是在 Windows执行的命令。 Windows: 使用visual studio commandprompt工具cl.exe cl -I"C:\Program Files\Java\jdk 也可以使用vc6.0直接建立动态库
编译的时候需要jni相关的头文件和库文件,在vc6.0的的搜索路径加入与java有关的两个路径即可即可 Tools->sptions->Directories
Linux:使用gcc工具 因为必须在 Java 虚拟机中执行所有 Java 代码,所以需要使用 Java 运行时环境。 完成这一步的方法之一是使用 java,它是随 SDK 一起提供的 Java 解释器。 所使用的命令是: java -cp . test.test_JNI_javaCallc_test 或者直接在eclipose中运行即可 输出:
【教程七】从 C/C++ 程序调用 Java 代码 JNI允许您从本机代码内调用 Java 类方法。 要做到这一点,通常必须使用 Invocation API 在本机代码内创建和初始化一个 JVM。 下列是您可能决定从 C/C++ 代码调用Java 代码的典型情况: 1.希望实现的这部分代码是平台无关的,它将用于跨多种平台使用的功能。 2.需要在本机应用程序中访问用 Java 语言编写的代码或代码库。 3.希望从本机代码利用标准 Java 类库。 从C/C++ 程序调用 Java代码的四个步骤: 1.编写 Java 代码。 这个步骤包含编写一个或多个 Java 类,这些类实现(或调用其它方法实现)您想要访问的功能。 2.编译 Java 代码。 在能够使用这些 Java 类之前,必须成功地将它们编译成字节码。 3.编写 C/C++ 代码。 这个代码将创建和实例化 JVM,并调用正确的 Java 方法。 4.运行本机 C/C++ 应用程序。 将运行应用程序以查看它是否正常工作。我们还将讨论一些用于处理常见错误的技巧。 [java] view plaincopy <!--[if !supportLists]-->1. <!--[endif]-->package test; <!--[if !supportLists]-->2. <!--[endif]--> <!--[if !supportLists]-->3. <!--[endif]-->public class JNI_cCalljava_test { <!--[if !supportLists]-->4. <!--[endif]--> <!--[if !supportLists]-->5. <!--[endif]--> public static int intMethod(int n) { <!--[if !supportLists]-->6. <!--[endif]--> return n*n; <!--[if !supportLists]-->7. <!--[endif]--> } <!--[if !supportLists]-->8. <!--[endif]--> <!--[if !supportLists]-->9. <!--[endif]--> public static boolean booleanMethod(boolean bool) { <!--[if !supportLists]-->10. <!--[endif]--> return !bool; <!--[if !supportLists]-->11. <!--[endif]--> } <!--[if !supportLists]-->12. <!--[endif]--> <!--[if !supportLists]-->13. <!--[endif]-->} 注:JNI_cCalljava_test.java 实现了两个 static Java 方法:intMethod(intn) 和 booleanMethod(boolean bool)(分别在第 3 行和第 7 行)。static方法是一种不需要与对象实例关联的类方法。调用 static方法要更容易些,因为不必实例化对象来调用它们。 接下来,我们将 Java 代码编译成字节码。 完成这一步的方法之一是使用随SDK 一起提供的Java 编译器 javac。使用的命令是: JNI_cCalljava_test.java 或者直接在eclipose中编写保存即可 步骤 3:编写 C/C++ 代码 即使是在本机应用程序中运行,所有 Java 字节码也必须在 JVM 中执行。 因此 C/C++ 应用程序必须包含用来创建和初始化 JVM 的调用。 为了方便我们,SDK 包含了作为共享库文件(jvm.dll 或 jvm.so)的 JVM,这个库文件可以嵌入到本机应用程序中。
让我们先从浏览一下 C 和 C++ 应用程序的整个代码开始,然后对两者进行比较。 带有嵌入式 JVM的 C 应用程序: [cpp] view plaincopy <!--[if !supportLists]-->1. <!--[endif]-->#include <jni.h> <!--[if !supportLists]-->2. <!--[endif]-->//jni.h文件包含在 C 代码中所需要的 JNI 的所有类型和函数定义 <!--[if !supportLists]-->3. <!--[endif]-->#ifdef _WIN32 <!--[if !supportLists]-->4. <!--[endif]-->#define PATH_SEPARATOR ';' <!--[if !supportLists]-->5. <!--[endif]-->#else <!--[if !supportLists]-->6. <!--[endif]-->#define PATH_SEPARATOR ':' <!--[if !supportLists]-->7. <!--[endif]-->#endif <!--[if !supportLists]-->8. <!--[endif]-->//1.包括准备本机应用程序以处理 Java 代码 <!--[if !supportLists]-->9. <!--[endif]-->//2.将 JVM 嵌入本机应用程序 <!--[if !supportLists]-->10. <!--[endif]-->//3.然后从该应用程序内找到并调用 Java 方法。 <!--[if !supportLists]-->11. <!--[endif]-->int main() <!--[if !supportLists]-->12. <!--[endif]-->{ <!--[if !supportLists]-->13. <!--[endif]-->/* <!--[if !supportLists]-->14. <!--[endif]-->接下来,声明所有希望在程序中使用的变量。 <!--[if !supportLists]-->15. <!--[endif]-->JavaVMOption options[] 具有用于 JVM 的各种选项设置。 <!--[if !supportLists]-->16. <!--[endif]-->当声明变量时,确保所声明的JavaVMOption options[] 数组足够大,以便能容纳您希望使用的所有选项。 <!--[if !supportLists]-->17. <!--[endif]-->在本例中,我们使用的唯一选项就是类路径选项。 <!--[if !supportLists]-->18. <!--[endif]-->因为在本示例中,我们所有的文件都在同一目录中,所以将类路径设置成当前目录。 <!--[if !supportLists]-->19. <!--[endif]-->可以设置类路径,使它指向任何您希望使用的目录结构。*/ <!--[if !supportLists]-->20. <!--[endif]--> JavaVMOption options[1]; <!--[if !supportLists]-->21. <!--[endif]--> JNIEnv *env; <!--[if !supportLists]-->22. <!--[endif]--> JavaVM *jvm; <!--[if !supportLists]-->23. <!--[endif]--> JavaVMInitArgs vm_args; <!--[if !supportLists]-->24. <!--[endif]-->/*JNIEnv *env 表示 JNI 执行环境。 <!--[if !supportLists]-->25. <!--[endif]-->JavaVM jvm 是指向 JVM 的指针,我们主要使用这个指针来创建、初始化和销毁 JVM。 <!--[if !supportLists]-->26. <!--[endif]-->JavaVMInitArgs vm_args 表示可以用来初始化 JVM 的各种 JVM 参数。*/ <!--[if !supportLists]-->27. <!--[endif]--> <!--[if !supportLists]-->28. <!--[endif]--> long status; <!--[if !supportLists]-->29. <!--[endif]--> jclass cls; <!--[if !supportLists]-->30. <!--[endif]--> jmethodID mid; <!--[if !supportLists]-->31. <!--[endif]--> jint square; <!--[if !supportLists]-->32. <!--[endif]--> jboolean not; <!--[if !supportLists]-->33. <!--[endif]--> <!--[if !supportLists]-->34. <!--[endif]-->/*avaVMInitArgs 结构表示用于 JVM 的初始化参数。 <!--[if !supportLists]-->35. <!--[endif]-->在执行 Java 代码之前,可以使用这些参数来定制运行时环境。 <!--[if !supportLists]-->36. <!--[endif]-->正如您所见,这些选项是一个参数,而 Java 版本是另一个参数。 <!--[if !supportLists]-->37. <!--[endif]-->按如下所示设置了这些参数:*/ <!--[if !supportLists]-->38. <!--[endif]--> <!--[if !supportLists]-->39. <!--[endif]-->/*为 JVM 设置类路径,以使它能找到所需要的 Java 类。 <!--[if !supportLists]-->40. <!--[endif]-->在这个特定示例中,因为 Sample2.class 和Sample2.exe 都位于同一目录中,所以将类路径设置成当前目录。 <!--[if !supportLists]-->41. <!--[endif]-->我们用来为 Sample2.c 设置类路径的代码如下所示:*/ <!--[if !supportLists]-->42. <!--[endif]--> options[0].optionString = "-Djava.class.path=."; <!--[if !supportLists]-->43. <!--[endif]--> memset(&vm_args, 0, sizeof(vm_args)); <!--[if !supportLists]-->44. <!--[endif]--> vm_args.version = JNI_VERSION_1_2; <!--[if !supportLists]-->45. <!--[endif]--> vm_args.nOptions = 1; <!--[if !supportLists]-->46. <!--[endif]--> vm_args.options = options; <!--[if !supportLists]-->47. <!--[endif]--> <!--[if !supportLists]-->48. <!--[endif]-->/*创建 JVM <!--[if !supportLists]-->49. <!--[endif]-->处理完所有设置之后,现在就准备创建 JVM 了。先从调用方法开始 <!--[if !supportLists]-->50. <!--[endif]-->如果成功,则这个方法返回零,否则,如果无法创建 JVM,则返回JNI_ERR。*/ <!--[if !supportLists]-->51. <!--[endif]--> status = JNI_CreateJavaVM(&jvm, (void**)&env, &vm_args); <!--[if !supportLists]-->52. <!--[endif]--> <!--[if !supportLists]-->53. <!--[endif]--> if (status != JNI_ERR) <!--[if !supportLists]-->54. <!--[endif]--> { <!--[if !supportLists]-->55. <!--[endif]-->/* <!--[if !supportLists]-->56. <!--[endif]-->查找并装入 Java 类 <!--[if !supportLists]-->57. <!--[endif]-->一旦创建了 JVM 之后,就可以准备开始在本机应用程序中运行 Java 代码。 <!--[if !supportLists]-->58. <!--[endif]-->首先,需要使用FindClass() 函数查找并装入 Java 类,如下所示: <!--[if !supportLists]-->59. <!--[endif]-->cls 变量存储执行FindClass() 函数后的结果,如果找到该类,则 cls 变量表示该Java 类的句柄, <!--[if !supportLists]-->60. <!--[endif]-->如果不能找到该类,则 cls 将为零。 <!--[if !supportLists]-->61. <!--[endif]-->*/ <!--[if !supportLists]-->62. <!--[endif]--> cls = (*env)->FindClass(env, "test/JNI_cCalljava_test"); <!--[if !supportLists]-->63. <!--[endif]--> printf("test1,cls=%d...\n",cls); <!--[if !supportLists]-->64. <!--[endif]--> <!--[if !supportLists]-->65. <!--[endif]--> if(cls !=0) <!--[if !supportLists]-->66. <!--[endif]--> { <!--[if !supportLists]-->67. <!--[endif]-->/* <!--[if !supportLists]-->68. <!--[endif]-->查找 Java 方法 <!--[if !supportLists]-->69. <!--[endif]-->接下来,我们希望用 GetStaticMethodID() 函数在该类中查找某个方法。 <!--[if !supportLists]-->70. <!--[endif]-->我们希望查找方法 intMethod,它接收一个 int 参数并返回一个 int。 <!--[if !supportLists]-->71. <!--[endif]-->以下是查找 intMethod 的代码: <!--[if !supportLists]-->72. <!--[endif]-->*/ <!--[if !supportLists]-->73. <!--[endif]--> mid = (*env)->GetStaticMethodID(env, cls, "intMethod", "(I)I"); <!--[if !supportLists]-->74. <!--[endif]-->/* <!--[if !supportLists]-->75. <!--[endif]-->mid 变量存储执行 GetStaticMethodID() 函数后的结果。 <!--[if !supportLists]-->76. <!--[endif]-->如果找到了该方法,则 mid 变量表示该方法的句柄。 <!--[if !supportLists]-->77. <!--[endif]-->如果不能找到该方法,则mid 将为零。 <!--[if !supportLists]-->78. <!--[endif]-->*/ <!--[if !supportLists]-->79. <!--[endif]--> if(mid !=0) <!--[if !supportLists]-->80. <!--[endif]--> { <!--[if !supportLists]-->81. <!--[endif]-->/*CallStaticIntMethod() 方法接受 cls(表示类)、mid(表示方法)以及用于该方法一个或多个参数。 <!--[if !supportLists]-->82. <!--[endif]-->在本例中参数是 int 5。*/ <!--[if !supportLists]-->83. <!--[endif]--> square = (*env)->CallStaticIntMethod(env, cls, mid, 5); <!--[if !supportLists]-->84. <!--[endif]--> printf("Result of intMethod: %d\n", square); <!--[if !supportLists]-->85. <!--[endif]--> } <!--[if !supportLists]-->86. <!--[endif]--> <!--[if !supportLists]-->87. <!--[endif]--> mid = (*env)->GetStaticMethodID(env, cls, "booleanMethod", "(Z)Z"); <!--[if !supportLists]-->88. <!--[endif]--> if(mid !=0) <!--[if !supportLists]-->89. <!--[endif]--> { <!--[if !supportLists]-->90. <!--[endif]--> not = (*env)->CallStaticBooleanMethod(env, cls, mid, 1); <!--[if !supportLists]-->91. <!--[endif]--> printf("Result of booleanMethod: %d\n", not); <!--[if !supportLists]-->92. <!--[endif]--> } <!--[if !supportLists]-->93. <!--[endif]--> } <!--[if !supportLists]-->94. <!--[endif]--> <!--[if !supportLists]-->95. <!--[endif]--> (*jvm)->DestroyJavaVM(jvm); <!--[if !supportLists]-->96. <!--[endif]--> return 0; <!--[if !supportLists]-->97. <!--[endif]--> } <!--[if !supportLists]-->98. <!--[endif]--> else <!--[if !supportLists]-->99. <!--[endif]--> return -1; <!--[if !supportLists]-->100. <!--[endif]-->}
带有嵌入式 JVM的 C++ 应用程序 [cpp] view plaincopy <!--[if !supportLists]-->1. <!--[endif]-->#include <jni.h> <!--[if !supportLists]-->2. <!--[endif]--> <!--[if !supportLists]-->3. <!--[endif]-->#ifdef _WIN32 <!--[if !supportLists]-->4. <!--[endif]-->#define PATH_SEPARATOR ';' <!--[if !supportLists]-->5. <!--[endif]-->#else <!--[if !supportLists]-->6. <!--[endif]-->#define PATH_SEPARATOR ':' <!--[if !supportLists]-->7. <!--[endif]-->#endif <!--[if !supportLists]-->8. <!--[endif]--> <!--[if !supportLists]-->9. <!--[endif]-->int main() <!--[if !supportLists]-->10. <!--[endif]-->{ <!--[if !supportLists]-->11. <!--[endif]--> JavaVMOption options[1]; <!--[if !supportLists]-->12. <!--[endif]--> JNIEnv *env; <!--[if !supportLists]-->13. <!--[endif]--> JavaVM *jvm; <!--[if !supportLists]-->14. <!--[endif]--> JavaVMInitArgs vm_args; <!--[if !supportLists]-->15. <!--[endif]--> long status; <!--[if !supportLists]-->16. <!--[endif]--> jclass cls; <!--[if !supportLists]-->17. <!--[endif]--> jmethodID mid; <!--[if !supportLists]-->18. <!--[endif]--> jint square; <!--[if !supportLists]-->19. <!--[endif]--> jboolean not; <!--[if !supportLists]-->20. <!--[endif]--> <!--[if !supportLists]-->21. <!--[endif]--> options[0].optionString = "-Djava.class.path=."; <!--[if !supportLists]-->22. <!--[endif]--> memset(&vm_args, 0, sizeof(vm_args)); <!--[if !supportLists]-->23. <!--[endif]--> vm_args.version = JNI_VERSION_1_2; <!--[if !supportLists]-->24. <!--[endif]--> vm_args.nOptions = 1; <!--[if !supportLists]-->25. <!--[endif]--> vm_args.options = options; <!--[if !supportLists]-->26. <!--[endif]--> status = JNI_CreateJavaVM(&jvm, (void**)&env, &vm_args); <!--[if !supportLists]-->27. <!--[endif]--> <!--[if !supportLists]-->28. <!--[endif]--> if (status != JNI_ERR) <!--[if !supportLists]-->29. <!--[endif]--> { <!--[if !supportLists]-->30. <!--[endif]--> cls = env->FindClass("Sample2"); <!--[if !supportLists]-->31. <!--[endif]--> if(cls !=0) <!--[if !supportLists]-->32. <!--[endif]--> { <!--[if !supportLists]-->33. <!--[endif]--> mid = env->GetStaticMethodID(cls, "intMethod", "(I)I"); <!--[if !supportLists]-->34. <!--[endif]--> if(mid !=0) <!--[if !supportLists]-->35. <!--[endif]--> { <!--[if !supportLists]-->36. <!--[endif]--> square = env->CallStaticIntMethod(cls, mid, 5); <!--[if !supportLists]-->37. <!--[endif]--> printf("Result of intMethod: %d\n", square); <!--[if !supportLists]-->38. <!--[endif]--> } <!--[if !supportLists]-->39. <!--[endif]--> <!--[if !supportLists]-->40. <!--[endif]--> mid = env->GetStaticMethodID(cls, "booleanMethod", "(Z)Z") <!--[if !supportLists]-->41. <!--[endif]--> if(mid !=0) <!--[if !supportLists]-->42. <!--[endif]--> { <!--[if !supportLists]-->43. <!--[endif]--> not = env->CallStaticBooleanMethod(cls, mid, 1); <!--[if !supportLists]-->44. <!--[endif]--> printf("Result of booleanMethod: %d\n", not); <!--[if !supportLists]-->45. <!--[endif]--> } <!--[if !supportLists]-->46. <!--[endif]--> } <!--[if !supportLists]-->47. <!--[endif]--> <!--[if !supportLists]-->48. <!--[endif]--> jvm->DestroyJavaVM(); <!--[if !supportLists]-->49. <!--[endif]--> return 0; <!--[if !supportLists]-->50. <!--[endif]--> } <!--[if !supportLists]-->51. <!--[endif]--> else <!--[if !supportLists]-->52. <!--[endif]--> return -1; <!--[if !supportLists]-->53. <!--[endif]--> }
C 和 C++ 实现的比较 C 和C++ 代码几乎相同;唯一的差异在于用来访问 JNI 函数的方法。 在 C 中,为了取出函数指针所引用的值,JNI 函数调用前要加一个(*env)-> 前缀。 在 C++ 中,JNIEnv类拥有处理函数指针查找的内联成员函数。 因此,虽然这两行代码访问同一函数,但每种语言都有各自的语法,如下所示。 C 语法: cls = (*env)->FindClass(env, "Sample2"); C++ 语法: cls = env->FindClass("Sample2");
C 语法: mid = (*env)->GetStaticMethodID(env, cls, "intMethod", "(I)I"); C++ 语法: mid = env->GetStaticMethodID(cls, "intMethod", "(I)I"); C 语法: square = env->CallStaticIntMethod(cls, mid, 5); C++ 语法: square = (*env)->CallStaticIntMethod(env, cls, mid, 5);
C 语法: (*jvm)->DestroyJavaVM(jvm); C++ 语法: jvm->DestroyJavaVM(); 步骤 4:运行应用程序 现在准备运行这个 C 应用程序,并确保代码正常工作。当运行 Sample2.exe 时,应该可以得到如下结果: windows: 使用vc6.0建一个普通的C语言工程 头文件路径设置同Java调用C语言里的设置 连接时需要jvm.lib支持 这里需要右击建立的工程,单击设置(Settings),link选项栏将数据库路径添加进来
C:"\Program Files"\Java\jdk 在下面的project options中加入以上语句,用空格隔开,programe Files用双引号引起来 运行时需要jvm.dll动态库的支持,需要在系统环境变量中增加以下路径: C:\Program Files\Java\jdk 方法:右击 我的电脑-》属性-》高级-》环境变量-》PATH 编辑,在原有环境变量的基础上增加以上路径,注意用";"号隔开 将eclipose生成的java代码放在JNI_cCalljava_test.exe同目录下(注意按照把报名文件夹也拷过去) E:\Sample2>JNI_cCalljava_test.exe
以上教程由凌阳教育Android培训讲师-徐哥提供,凌阳教育专注于嵌入式Linux培训,Android培训领域,拥有十多年的高校嵌入式师资培训经验,成为中国高校嵌入式培训第一品牌! 欢迎访问凌阳教育官方网站:http://www.
|
|
来自: 昵称14797374 > 《JNI》