Title: Java Native Interface JNI
1Java Native Interface (JNI)
- SENG 609.04 Design Patterns
- Winter, 2003
- Presented by Guy Davis, Ming Zhou
2Agenda
- Overview
- Justification
- Hello World!
- Summary
- References
- QA
3JNI Overview
4Interactions with Native Code
Access to Java world from native code
Access to native code from Java
5Justification
- 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
6HelloWorld.java
- class HelloWorld
- public native void displayHelloWorld()
- static
- System.loadLibrary("hello")
-
- public static void main(String args)
- new HelloWorld().displayHelloWorld()
-
7HelloWorld.java
- class HelloWorld
- public native void displayHelloWorld()
- static
- System.loadLibrary("hello")
-
- public static void main(String args)
- new HelloWorld().displayHelloWorld()
-
8HelloWorld.java
- class HelloWorld
- public native void displayHelloWorld()
- static
- System.loadLibrary("hello")
-
- public static void main(String args)
- new HelloWorld().displayHelloWorld()
-
9HelloWorld.java
- class HelloWorld
- public native void displayHelloWorld()
- static
- System.loadLibrary("hello")
-
- public static void main(String args)
- new HelloWorld().displayHelloWorld()
-
10HelloWorld.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
11HelloWorld.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
12HelloWorld.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
13HelloWorldImp.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
14HelloWorldImp.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
15HelloWorldImp.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
16HelloWorldImp.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
17Create 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
18Run 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)
19Summary
- 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
20References
- 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
21Questions?