Let’s talkabout JNIYongqiang Li
Agenda JNI Overview JNI Sample Step by Step More Details of JNI JVM Native Memory Reference
JNI (Java Native Interface)Overview                   Linux C/C++ Side Java Side                   Windows C/C++ Side(Code...
Java –verbose:jni HelloWorld
JNI Sample (Step 1) Define       the Java class and native functions
Step 2 Use “javah” to generate the header for native functions.
Header File
Step 3 Implement   .c/.cpp
Step 4 Build   the native code     g++ -I/usr/lib/java/include –      I/usr/lib/java/include/linux -share      org_yli_j...
Step 5 Run   it!    java -Djava.library.path=. SampleJNI
A Real Case – Use Java tostart GTK+ application.
More Details of JNI JNI   Type
JNIEnv Definea set of functions to manipulate JNI data, get field/method id, find class.    env->GetVersion()    env->T...
Fields Can’t    access  directly Need to get Field  ID first
Methods Method ID is needed, just like fields.
Use “javap” to find out the signature.
And Constructor Use   GetMethodID as well But method name is special. It’s “<init>”. Still use “javap” to find out its ...
 As   well, jstring can’t be manipulated directly.       jstring• Depends on JNIEnv Functions   •   GetStringChars/GetStr...
Other things depends onJNIEnv Manage   Reference Find Class Access Array Throw exception…
Native Memory
Consume native memory
More topics… Launch  JVM in C/C++ Traps and pitfalls
Reference Java Native Interface Specification Thanks for the memory
Let's talk about jni
Let's talk about jni
Let's talk about jni
Let's talk about jni
Let's talk about jni
Let's talk about jni
Let's talk about jni
Let's talk about jni
Let's talk about jni
Let's talk about jni
Let's talk about jni
Let's talk about jni
Upcoming SlideShare
Loading in...5
×

Let's talk about jni

680

Published on

Brief introduction to JNI

Published in: Technology, Education
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
680
On Slideshare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
0
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

Let's talk about jni

  1. 1. Let’s talkabout JNIYongqiang Li
  2. 2. Agenda JNI Overview JNI Sample Step by Step More Details of JNI JVM Native Memory Reference
  3. 3. JNI (Java Native Interface)Overview Linux C/C++ Side Java Side Windows C/C++ Side(Code, VM) Mac OS C/C++ Side
  4. 4. Java –verbose:jni HelloWorld
  5. 5. JNI Sample (Step 1) Define the Java class and native functions
  6. 6. Step 2 Use “javah” to generate the header for native functions.
  7. 7. Header File
  8. 8. Step 3 Implement .c/.cpp
  9. 9. Step 4 Build the native code  g++ -I/usr/lib/java/include – I/usr/lib/java/include/linux -share org_yli_jni_SampleJNI.cpp -o libNative.so
  10. 10. Step 5 Run it!  java -Djava.library.path=. SampleJNI
  11. 11. A Real Case – Use Java tostart GTK+ application.
  12. 12. More Details of JNI JNI Type
  13. 13. JNIEnv Definea set of functions to manipulate JNI data, get field/method id, find class.  env->GetVersion()  env->Throw()  env->FindClass()  env->NewLocalRef()a Thread Local Storage variable
  14. 14. Fields Can’t access directly Need to get Field ID first
  15. 15. Methods Method ID is needed, just like fields.
  16. 16. Use “javap” to find out the signature.
  17. 17. And Constructor Use GetMethodID as well But method name is special. It’s “<init>”. Still use “javap” to find out its signature. Need use env->NewObject() to create a new instance.
  18. 18.  As well, jstring can’t be manipulated directly. jstring• Depends on JNIEnv Functions • GetStringChars/GetStringUTFChars • ReleaseStringChars/ReleaseStringUTFChars • GetStringLength/GetStringUTFLength • NewString/NewStringUTF
  19. 19. Other things depends onJNIEnv Manage Reference Find Class Access Array Throw exception…
  20. 20. Native Memory
  21. 21. Consume native memory
  22. 22. More topics… Launch JVM in C/C++ Traps and pitfalls
  23. 23. Reference Java Native Interface Specification Thanks for the memory

×