Documente Academic
Documente Profesional
Documente Cultură
Budditha Hettige
http://www.dscs.sjp.ac.lk/~budditha/index.html
Budditha Hettige
http://www.dscs.sjp.ac.lk/~budditha/index.html
JNI Overview
Budditha Hettige
http://www.dscs.sjp.ac.lk/~budditha/index.html
Disadvantages
Write Once Run Anywhere is not possible Run time errors debugging is difficult in native code An applet can not call a native method Security risk is potential
6
Budditha Hettige
http://www.dscs.sjp.ac.lk/~budditha/index.html
Function format
JNIEXPORT void JNICALL Java_ClassName_MethodName (JNIEnv *env, jobject obj) { /*Implement Native Method Here*/ }
Budditha Hettige
http://www.dscs.sjp.ac.lk/~budditha/index.html
System.loadLibrary("hello");
} public static void main(String[] args) { new HelloWorld().displayHelloWorld(); } }
Invoke native Method
Budditha Hettige
http://www.dscs.sjp.ac.lk/~budditha/index.html
Generate HelloWorld.h
javah HelloWorld
Budditha Hettige
http://www.dscs.sjp.ac.lk/~budditha/index.html
10
Sample: 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_displayHelloWorld(JNIEnv *env, jobject); #ifdef __cplusplus } #endif #endif
HelloWorld.C
#include <jni.h> #include "HelloWorld.h" #include <stdio.h> JNIEXPORT void JNICALL Java_HelloWorld_displayHelloWorld(JNIEnv *env, jobject obj) { printf("Hello world!\n"); return; }
12
Data types
Java Type boolean byte char short int long float double void
Budditha Hettige
http://www.dscs.sjp.ac.lk/~budditha/index.html
Native Type jboolean jbyte jchar jshort jint jlong jfloat jdouble void
14
Object Types
Budditha Hettige
http://www.dscs.sjp.ac.lk/~budditha/index.html
15
Output file
16
Result:
Possible exceptions:
java.lang.UnsatisfiedLinkError: no hello in shared library path at java.lang.Runtime.loadLibrary(Runtime.java) at java.lang.System.loadLibrary(System.java) at java.lang.Thread.init(Thread.java)
17
Example
Create a .dll file named jm.dll Implement the following methods
printMessage(): Print the message User define java native interface Jsum(int, int): Return the summation of the given two integer values Jmul(int, int): Return the multiplication of the given two integer values
Create a java class named JMaths.java to implement the above Native methods Create a class named Calculator.java to run the program (use JMaths class)
Budditha Hettige
http://www.dscs.sjp.ac.lk/~budditha/index.html
18
Answer:
JMaths.java class JMaths { public native void printMessage(); public native int jsum(int x, int y); public native int jmul(int c, int g); public void calculate() { System.out.println("JSUM " + jsum(2,4)); System.out.println("JMUL " + jmul(2,4)); } static { System.loadLibrary("jm"); }}
Budditha Hettige
http://www.dscs.sjp.ac.lk/~budditha/index.html
Calculator.java public class Calculator { public static void main(String[] args) { JMaths j = new JMaths(); j.calculate(); } }
19
Budditha Hettige
http://www.dscs.sjp.ac.lk/~budditha/index.html
20
Answers
JMaths.c
#include <jni.h> #include <stdio.h> #include JMaths.h JNIEXPORT void JNICALL Java_JMaths_printMessage(JNIEnv *env, jobject obj) { printf("Java JNI Mathamatics!\n"); return; } JNIEXPORT jint JNICALL Java_JMaths_jsum(JNIEnv *env, jobject obj, jint x, jint y) { return x + y; } JNIEXPORT jint JNICALL Java_JMaths_jmul(JNIEnv *env, jobject obj, jint x, jint y) { return x * y; } Budditha Hettige 21 http://www.dscs.sjp.ac.lk/~budditha/index.html
Files
Jmaths.c : Source files for the DLL Jmaths.java: Java class for native interface Jmaths.h: C Header file for the native interface Calculator.java: Calculator class Jm.dll: Created DLL file
Budditha Hettige
http://www.dscs.sjp.ac.lk/~budditha/index.html
22
Commands
Javac Jmath.java Javah Jmaths Cd C:\Program Files\CodeBlocks\MinGW gcc -Wall -D_JNI_IMPLEMENTATION_ -Wl,--kill-at -I C:/Program Files/Java/jdk1.6.0_02/include -I C:/Program Files/Java/jdk1.6.0_02/include shared c:/budditha/JavaMaths.c -o jm.dll Javac Calculator.java Java Calculator
Budditha Hettige
http://www.dscs.sjp.ac.lk/~budditha/index.html
23
24