Sunteți pe pagina 1din 24

CSC 308 2.

System Development with Java

Budditha Hettige Department of Statistics and Computer Science

JAVA Native Interface

Budditha Hettige
http://www.dscs.sjp.ac.lk/~budditha/index.html

Java Native Interface


Is a programming framework JNI functions written in a language other than Java They could be C, C++, or even Assembly
Native methods are compiled into a dynamic link library (.dll, .so, etc.) Operating System loads and links the library into the process that is running the Java Virtual Machine

Budditha Hettige
http://www.dscs.sjp.ac.lk/~budditha/index.html

JNI Overview

Budditha Hettige
http://www.dscs.sjp.ac.lk/~budditha/index.html

Why Native Interface?


Some functionality are not provided by java Low-level drives/devices specific to OS Increase performance by implementing rigorous tasks in native language Java is slow in general and may not be suitable for some functions (e.g., image compression and decompression) Try to use existing legacy library and could not afford to rewrite it in java JNI can be used as a wrapper of these legacy codes Can slowly migrate legacy code to a newer platform Improve efficiency of integration TCP/IP sockets can be used, but there are overhead in data transmission
Budditha Hettige
http://www.dscs.sjp.ac.lk/~budditha/index.html

Java Native Interface contd.


Advantages
Use the existing library that was previously written in another language. Use of windows API functions Increasing the speed of execution Invoke API functions from server product that is developed in C or C++ from a java client.

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

How JNI works?


Native functions are implemented in separate .c or .cpp files JVM invokes the function
it passes a JNIEnv pointer, a jobject pointer, and any Java arguments declared by the Java method The env pointer is a structure that contains the interface to the JVM

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

JNI Development Steps


1. Declare native methods in the java class 2. Compile the java class 3. Generate C header for the java class 4. Create C file (for DLL) 5. Compile and Create DLL file 6. Copy DLL to Java Path 7. RUN the JAVA Program
Budditha Hettige
http://www.dscs.sjp.ac.lk/~budditha/index.html

Declare native methods


Example : HelloWorld.java
Native Method

class HelloWorld { public native void displayHelloWorld();


static {
Call hello.dll file

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 JNI Header


Compile HelloWorld.java
javac HelloWorld.java

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

The JVM reference

The calling object


11

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

Size in bits 8, unsigned 8 16, unsigned 16 32 64 32 64 n/a


13

JNI Data Mapping


Example 01: class Prompt { private native String getLine(String prompt); } JNIEXPORT jstring JNICALL Java_Prompt_getLine(JNIEnv *, jobject, jstring); Example 02:

public native int jsum(int x, int y);


JNIEXPORT jint JNICALL Java_JMaths_jsum(JNIEnv *env, jobject obj, jint x, jint y) { return x + y; }
Budditha Hettige
http://www.dscs.sjp.ac.lk/~budditha/index.html

14

Object Types

Budditha Hettige
http://www.dscs.sjp.ac.lk/~budditha/index.html

15

Create a DLL (minGW gcc)


Type the following command (Command prompt)
Gcc -Wall -D_JNI_IMPLEMENTATION_ -Wl,--kill-at

-I HederLibariers -shared C_program -o output_file


Example: 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/win32 -shared c:/budditha/helloWord.c -o c:/budditha/hw.dll
Input file
Budditha Hettige
http://www.dscs.sjp.ac.lk/~budditha/index.html

Output file
16

Run the Program


Command:

Result:

java HelloWorld Hello World!

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)

Copy hello.dll to:


C:\Program Files\Java\jdk1.6.0_02\jre\bin\
Budditha Hettige
http://www.dscs.sjp.ac.lk/~budditha/index.html

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

Generate a C header file


Compile the JMaths.java
Javac JMaths.java
/* DO NOT EDIT THIS FILE - it is machine generated */ #include <jni.h> /* Header for class JMaths */ #ifndef _Included_JMaths #define _Included_JMaths #ifdef __cplusplus extern "C" { #endif /* * Class: JMaths * Method: printMessage * Signature: ()V */ JNIEXPORT void JNICALL Java_JMaths_printMessage (JNIEnv *, jobject); /* * Class: JMaths * Method: jsum * Signature: (II)I */ JNIEXPORT jint JNICALL Java_JMaths_jsum (JNIEnv *, jobject, jint, jint); /* * Class: JMaths * Method: jmul * Signature: (II)I */ JNIEXPORT jint JNICALL Java_JMaths_jmul (JNIEnv *, jobject, jint, jint); #ifdef __cplusplus } #endif #endif

Create a header file


Javah Jmaths

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

Useful Tips for JNI developers


Install Code::block for C programming
Download: http://www.codeblocks.org

Install JDK Install Textpad for Java based programming


Download: http://www.textpad.com

Use gcc compilers (Use MinGW gcc compiler on code::block)


C:\Program Files\CodeBlocks\MinGW\bin
Budditha Hettige
http://www.dscs.sjp.ac.lk/~budditha/index.html

24

S-ar putea să vă placă și