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

Let's talk about jni

on

  • 799 views

Brief introduction to JNI

Brief introduction to JNI

Statistics

Views

Total Views
799
Views on SlideShare
679
Embed Views
120

Actions

Likes
2
Downloads
0
Comments
0

4 Embeds 120

http://www.bloodlee.com 109
http://translate.googleusercontent.com 6
http://www.linkedin.com 3
http://www.innoli.com 2

Accessibility

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Let's talk about jni Let's talk about jni Presentation Transcript

  • 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, VM) Mac OS C/C++ Side
  • 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_jni_SampleJNI.cpp -o libNative.so
  • 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->Throw()  env->FindClass()  env->NewLocalRef()a Thread Local Storage variable
  • 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 signature. Need use env->NewObject() to create a new instance.
  •  As well, jstring can’t be manipulated directly. jstring• Depends on JNIEnv Functions • GetStringChars/GetStringUTFChars • ReleaseStringChars/ReleaseStringUTFChars • GetStringLength/GetStringUTFLength • NewString/NewStringUTF
  • 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