Java Native Interface JNI - PowerPoint PPT Presentation

1 / 21
About This Presentation
Title:

Java Native Interface JNI

Description:

et al., Tricks of the Java Programming Gurus, 1996, http://docs.rinet.ru:8080/JaTricks ... Java Native Interface, Canadian Mind Products, 2002, http://mindprod. ... – PowerPoint PPT presentation

Number of Views:278
Avg rating:3.0/5.0
Slides: 22
Provided by: GuyD1
Category:
Tags: jni | interface | java | mind | native | tricks

less

Transcript and Presenter's Notes

Title: Java Native Interface JNI


1
Java Native Interface (JNI)
  • SENG 609.04 Design Patterns
  • Winter, 2003
  • Presented by Guy Davis, Ming Zhou

2
Agenda
  • Overview
  • Justification
  • Hello World!
  • Summary
  • References
  • QA

3
JNI Overview
4
Interactions with Native Code
Access to Java world from native code
Access to native code from Java
5
Justification
  • Pros
  • Reuse allows access to useful native code
  • Efficiency use best language for the task
  • Cons
  • Applets doesn't work as they're mobile
  • Portability native methods aren't portable
  • Extra work javah, create shared native libs

6
HelloWorld.java
  • class HelloWorld
  • public native void displayHelloWorld()
  • static
  • System.loadLibrary("hello")
  • public static void main(String args)
  • new HelloWorld().displayHelloWorld()

7
HelloWorld.java
  • class HelloWorld
  • public native void displayHelloWorld()
  • static
  • System.loadLibrary("hello")
  • public static void main(String args)
  • new HelloWorld().displayHelloWorld()

8
HelloWorld.java
  • class HelloWorld
  • public native void displayHelloWorld()
  • static
  • System.loadLibrary("hello")
  • public static void main(String args)
  • new HelloWorld().displayHelloWorld()

9
HelloWorld.java
  • class HelloWorld
  • public native void displayHelloWorld()
  • static
  • System.loadLibrary("hello")
  • public static void main(String args)
  • new HelloWorld().displayHelloWorld()

10
HelloWorld.h
include jni.h / Header for class HelloWorld
/ ifndef _Included_HelloWorld define
_Included_HelloWorld ifdef __cplusplus
extern C endif / Class HelloWorld
Method displayHelloWorld Signature ()V
/ JNIEXPORT void JNICALL Java_HelloWorld_display
HelloWorld(JNIEnv , jobject) ifdef
__cplusplus endif endif
11
HelloWorld.h
include jni.h / Header for class HelloWorld
/ ifndef _Included_HelloWorld define
_Included_HelloWorld ifdef __cplusplus
extern C endif / Class HelloWorld
Method displayHelloWorld Signature ()V
/ JNIEXPORT void JNICALL Java_HelloWorld_display
HelloWorld(JNIEnv , jobject) ifdef
__cplusplus endif endif
12
HelloWorld.h
include jni.h / Header for class HelloWorld
/ ifndef _Included_HelloWorld define
_Included_HelloWorld ifdef __cplusplus
extern C endif / Class HelloWorld
Method displayHelloWorld Signature ()V
/ JNIEXPORT void JNICALL Java_HelloWorld_display
HelloWorld(JNIEnv , jobject) ifdef
__cplusplus endif endif
13
HelloWorldImp.c
include ltjni.hgt include "HelloWorld.h" include
ltstdio.hgt JNIEXPORT void JNICALL
Java_HelloWorld_displayHelloWorld(JNIEnv env,
jobject obj) printf("Hello world!\n")
return
14
HelloWorldImp.c
include ltjni.hgt include "HelloWorld.h" include
ltstdio.hgt JNIEXPORT void JNICALL
Java_HelloWorld_displayHelloWorld(JNIEnv env,
jobject obj) printf("Hello world!\n")
return
15
HelloWorldImp.c
include ltjni.hgt include "HelloWorld.h" include
ltstdio.hgt JNIEXPORT void JNICALL
Java_HelloWorld_displayHelloWorld(JNIEnv env,
jobject obj) printf("Hello world!\n")
return
16
HelloWorldImp.c
include ltjni.hgt include "HelloWorld.h" include
ltstdio.hgt JNIEXPORT void JNICALL
Java_HelloWorld_displayHelloWorld(JNIEnv env,
jobject obj) printf("Hello world!\n")
return
17
Create a Shared Library
  • class HelloWorld
  • . . .
  • System.loadLibrary("hello")
  • . . .
  • Compile the native code into a shared library
  • (example for MS Windows Visual C 4.0)
  • cl -Ic\java\include -Ic\java\include\win32
  • -LD HelloWorldImp.c -Fehello.dll

18
Run the Program
  • Command
  • java HelloWorld
  • Result
  • Hello World!
  • Possible exceptions
  • java.lang.UnsatisfiedLinkError no hello in
    shared library path at java.lang.Runtime.loadLibra
    ry(Runtime.java) at java.lang.System.loadLibrary(S
    ystem.java) at java.lang.Thread.init(Thread.java)

19
Summary
  • Connect Java with native languages
  • Code reuse
  • Powerful
  • Compromise of Java safety features,
    cross-platform and dynamic ability
  • JNI call is slow and costful
  • Not work well for applets

20
References
  • Glenn L. Vanderburg. et al., Tricks of the Java
    Programming Gurus, 1996, http//docs.rinet.ru8080
    /JaTricks/
  • Beth Stearns, Trail Java Native Interface, Sun
    Microsystems, Inc., 2003, http//java.sun.com/docs
    /books/tutorial/native1.1/
  • Roedy Green, JNI, The Java Native Interface,
    Canadian Mind Products, 2002, http//mindprod.com/
    jni.html
  • Kong Wenyu, A Brief Introduction to Java Native
    Interface, http//www.geocities.com/kongwenyu/jni.
    html

21
Questions?
Write a Comment
User Comments (0)
About PowerShow.com