• Like
  • Save
Let's talk about jni
Upcoming SlideShare
Loading in...5

Let's talk about jni

Uploaded on

Brief introduction to JNI

Brief introduction to JNI

More in: Technology , Education
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
No Downloads


Total Views
On Slideshare
From Embeds
Number of Embeds



Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

    No notes for slide


  • 1. Let’s talkabout JNIYongqiang Li
  • 2. Agenda JNI Overview JNI Sample Step by Step More Details of JNI JVM Native Memory Reference
  • 3. JNI (Java Native Interface)Overview Linux C/C++ Side Java Side Windows C/C++ Side(Code, VM) Mac OS C/C++ Side
  • 4. Java –verbose:jni HelloWorld
  • 5. JNI Sample (Step 1) Define the Java class and native functions
  • 6. Step 2 Use “javah” to generate the header for native functions.
  • 7. Header File
  • 8. Step 3 Implement .c/.cpp
  • 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. Step 5 Run it!  java -Djava.library.path=. SampleJNI
  • 11. A Real Case – Use Java tostart GTK+ application.
  • 12. More Details of JNI JNI Type
  • 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. Fields Can’t access directly Need to get Field ID first
  • 15. Methods Method ID is needed, just like fields.
  • 16. Use “javap” to find out the signature.
  • 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.  As well, jstring can’t be manipulated directly. jstring• Depends on JNIEnv Functions • GetStringChars/GetStringUTFChars • ReleaseStringChars/ReleaseStringUTFChars • GetStringLength/GetStringUTFLength • NewString/NewStringUTF
  • 19. Other things depends onJNIEnv Manage Reference Find Class Access Array Throw exception…
  • 20. Native Memory
  • 21. Consume native memory
  • 22. More topics… Launch JVM in C/C++ Traps and pitfalls
  • 23. Reference Java Native Interface Specification Thanks for the memory