Java Introduction to JNI Prepared by Humaira Siddiqui
What is JNI? -1 n JNI stands for Java Native Interface n JNI specifies a communication protocol between Java code and external, native code. n It enables your Java code to interface with native code written in other languages (such as C, C++) n Java native methods are methods declared in your Java code (much like you declare an abstract method), but which are actually implemented in another programming language
What is JNI? -2 n JNI allows Java programmers to n Leverage the improved speed possible with natively compiled code (such as C or assembler). For example, you might need an extremely fast math routine for a scientific application or game program. n Utilize existing code libraries of native code in your Java programs. For example, there might be a really good file compression library written in C. Why try to rewrite it in Java when you can access it (as is) using JNI?
What is JNI? -3 n JNI Drawbacks n Your program is no longer platform independent n Your program is not as robust. If there is a null pointer exception in your native code, the JVM can't display a helpful message. It might even lock up.
What is JNI? -4 n JNI supports n Native methods can create and manipulate Java objects such as strings and arrays. n Native methods can manipulate and update Java objects passed into them (as parameters) n You can catch and throw exceptions from native methods and handle these exceptions in either the native method or your Java application n This almost seamless sharing of objects makes it very easy to incorporate native methods in your Java code
Writing JNI Methods -1 n You write JNI programs for C/C++ by doing the following 1. Create a Java class that declares the native method, contains code for loading the native library. The class should also contain a main method which calls the native method 2. Compile the Java class 3. Run the javah tool with the -jni option to generate a header file for your native C/C++ code 4. Write the C/C++ code (file) which implements the method defined in the header file 5. Compile the header and C/C++ code into a library (a DLL under Windows) 6. Run the Java class (main method)
Writing JNI Methods Write Your Java class package com.cexp.wms.jni.examples; class HelloWorld { public native void displayHello(); //native method { System.loadLibrary("c_library"); } public static void main(String[] args) { HelloWorld hw = new HelloWorld(); hw.displayHello(); } }
Writing JNI Methods Compile your class javac HelloWorld.java
Writing JNI Methods Create the header (.h) file by using the javah tool javah -jni -o HelloWorld.h -classpath. com.cexp.jni.examples.HelloWorld (Note: entire command must be on one line) This command will create a file called HelloWorld.h which contains a C function signature for implementing the native method. The C header file will include, the standard header file for JNI applications.
Writing JNI Methods The header file it creates will look something like /* DO NOT EDIT THIS FILE - it is machine generated */ #include /* Header for class HelloWorld */ #ifndef _Included_HelloWorld #define _Included_HelloWorld #ifdef __cplusplus extern "C" { #endif JNIEXPORT void JNICALL Java_com_cexp_wms_jni_examples_HelloWorld_displayHello (JNIEnv *, jobject); #ifdef __cplusplus } #endif
Writing JNI Methods -6 n The displayHello routine has two parameters (standard for all JNI native methods). n JNI Env * -- JNI interface pointer. We can use this pointer to make useful calls like GetStringUTFChars(), which allows us to convert Java Unicode (2 byte) strings to UTF-8 (1 byte) strings. n jobject -- A reference to the (calling) object itself. Similar to this
Writing JNI Methods -7 n JNI native methods can access the following types boolean jboolean string jstring byte jbyte char jchar short jshort int jint long jlong float jfloat double jdouble void
Writing JNI Methods -8 n All of these types can be accessed directly except for jstring, which requires a subroutine call to in order to convert a Java Unicode string (2 bytes) to a C-style char* string (1 byte UTF-8 format).
Writing JNI Methods Write the native C/C++ code which implements the method. Use the same signature that your header file uses. You might name your file something like "HelloWorld.c". #include #include "HelloWorld.h" #include JNIEXPORT void JNICALL Java_com_cexp_wms_jni_examples_HelloWorld_displayHello (JNIEnv *env, jobject obj) { printf("Hello world! I'm here! \n"); return; }
Writing JNI Methods Compile the C/C++ code into a library (a DLL if your code will run under Windows). If you use C++ Builder to compile your library under Windows, make sure you create a DLL project and then add the C/C++ file to it (e.g. HelloWorld.c). You'll need to add the following to your compiler's include path: \javadir\include \javadir\include\win32 Be sure to name your project c_library so the DLL it creates will be named c_library.dll.
Writing JNI Methods If you are compiling your C library under Windows using Visual C++, you do the following: cl -Ic:\javadir\include -Ic:\javadir\include\win32 -LD HelloWorld.c -Fec_library.dll (Note: entire command must be on one line) This creates the file, c_library.dll
Writing JNI Methods If you are compiling your C library under Solaris, you do the following: cc -G -I/javadir/include -I/javadir/include/solaris \ HelloWorld.c -o c_library.so This creates the file, c_library.so
Writing JNI Methods Run the Java class (main method). Switch to the directory just above the "com" directory and run the following: java -classpath. com.cexp.wms.jni.examples.HelloWorld You should see "Hello world! I'm here!" appear on the screen! If you see a "java.lang.UnsatisfiedLinkError" error message, then Java couldn't find your shared library. Either add the directory your library (DLL) resides in to your Java runtime library path OR copy the library file to the working directory of your Java program.
JNI and Fortran n You can invoke Fortran code from your Java applications n To do that, you write some Java JNI code to invoke C/C++ methods in a DLL, and write those C/C++ methods to invoke the Fortran code you want to run n It sounds complicated, but the runtime overhead isn’t as bad as you might think JNI (Java program) C/C++ “bridge” DLL Fortran program
JNI Summary n JNI can be a little tedious the first time you try it, but in the scheme of things it isn't that complicated. n Use JNI to n leverage existing code (that you don't want to port to Java) n Solve problems that Java isn't suited for (e.g., when you need the speed of C or assembler, or when you need to write low level code to communicate directly with hardware