JNI Index
Introduction, Purpose and features
JNI allows Java programmers to
JNI Drawbacks
JNI supports
The Role of JNI

Let’s create the JNI “Hello World” application – a Java application that calls a C function via JNI to print “Hello World!”.
The process consists of the following steps:
The program defines a class named HelloWorld that contains a native method print().
1. Create a class (HelloWorld.java)
class HelloWorld { public native void print(); //native method static //static initializer code { System.loadLibrary("CLibHelloWorld"); } public static void main(String[] args) { HelloWorld hw = new HelloWorld(); hw.print(); } } |
There are two remarkable things here.
1. First one is the declaration of a native method using the keyword native.
public native void print(); |
This tells the compiler that print() will be accessed from an external library, and that it should not look for it in the Java source code. Accordingly, notice that there is no implementation of this method present in the Java code.
2. The second remarkable section of code is the following:
static { System.loadLibrary(“CLibHelloWorld"); //**** Loads CLibHelloWorld.dll } |
• Before the native method print() can be called, the native library that implements print must be loaded. The code above loads the native library in the static initializer of the HelloWorld class.
• The Java VM automatically runs the static initializer before invoking any methods in the CLibHelloWorld class.
Next we compile the JNI HelloWorld java application as follows:
Having the application compiled we can use the javah tool to generate the header (h) file that declares the native method print().
javah –jni HelloWorld |
/* 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 /* * Class: HelloWorld * Method: print * Signature: ()V */ JNIEXPORT void JNICALL Java_HelloWorld_print (JNIEnv *, jobject); #ifdef __cplusplus } #endif #endif |
In the below line
JNIEXPORT void JNICALL Java_HelloWorld_print (JNIEnv *, jobject); |
• The method name is prefixed with Java_ and then the full name of the class to which it belongs, then the function name.
• This is to distinguish it from methods that belong to other classes and might have the same name.
• The first argument for every native method implementation is a JNIEnv interface pointer.
• The second argument is a reference to the HelloWorld object itself.
Writing the implementation:
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 “CLibHelloWorld.c”.
#include "HelloWorld.h" #include "jni.h" #include "stdio.h" JNIEXPORT void JNICALL Java_HelloWorld_print(JNIEnv *env, jobject obj) { printf("Hello world\n"); return; } |
Here
Compile the C/C++ code into a library (a DLL if your code will run under Windows). – Click here to read step by step procedure to create DLL project using Visula Studio.
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. CLibHelloWorld.c). You’ll need to add the following to your compiler’s include path:
Be sure to name your project CLibHelloWorld so the DLL it creates will be named c_library.dll.
Run the Java class (main method). Be sure all the files and dll are in one folder.
java HelloWorld |
You should see “Hello world” appear on the screen!
If you see a “java.lang.UnsatisfiedLinkError” error message, then Java couldn’t find your shared library OR mismatch in native functions.
Description :
This is the one stop educational site for all Electronic and Computer students. If you want to learn something new then we are here to help. We work on Microcontroller projects, Basic Electronics, Digital electronics, Computer projects and also in basic c/c++ programs.
#Home #Sitemap #Resources #Terms of Use
Copyright©2012 electrofriends.com All Rights Reserved
Contact:info@electrofriends.com
Now a days, its very difficult to findout good jobs… but your blog helping us to findout best jobs….!!! Well done..!!
Your blog helped me understand the concept easily. Thanks for posting.
very good blob. clear presentation to be understood
Thanks for writing such a blog With smooth flow and concept .
Thankx a lot u have solve my major problm…