Wednesday, March 3, 2010

java JNI sample implementation

1. First create A simple java class.

public class HelloWorld {
public native void Hello();

static {

public static void main(String[] args) {
new HelloWorld().Hello();


use load() insted of loadlibrary() and give the absolute path of dll in the load function


now compile this you will get a HelloWorld.class file
it will show loaded and a error message UnsatasifiedLinkError
on the Hello() Native method
there is no need to worry about continue ahead you will find why it is showing this when you be able to run this


now Use javah command on a command prompt to generate the .h file
javah HelloWorld it will give you a .h file .h file will look like this


/ DO NOT EDIT THIS FILE - it is machine generated /
/ Header for class HelloWorld /

#ifndef _Included_HelloWorld
#define _Included_HelloWorld
#ifdef __cplusplus
extern "C" {
Class: HelloWorld
Method: Hello
Signature: ()V
JNIEXPORT void JNICALL Java_HelloWorld_Hello
, jobject);

#ifdef __cplusplus


Now you have a .h file use this to create a HelloWorld.c file which is in form of c
it will look like this

#include "HelloWorld.h"

JNIEXPORT void JNICALL Java_HelloWorld_Hello(JNIEnv env , jobject obj)
printf("Hello world!\n");

pay attention to JNIEXPORT void JNICALL Java_HelloWorld_Hello(JNIEnv
, jobject) call you have to modify this when you write .c file


Now the important step come in you need to download mingw software so that you can run gcc command

once you have this in place

you go to command prompt and follow these steps(depends upon where you install mingw)
C:\>Cd mingw\bin

now you need to use following command to generate .o file(i show you how)

gcc -c -I"C:\program files\Java\jdk1.5.0\include" -I"C:\Program Files\Java\jdk1.5.0\include\

win32" -o "C:\calldll\HelloWorld.o" "C:\calldll\HelloWorld.c"

you have to run this command on


this will create HelloWorld.o file

-I"C:\program files\Java\jdk1.5.0\include" in this you need to mention your PATH of jdk in my case this is (C:\program files\Java\jdk1.5.0\include)

and in

-o "C:\calldll\HelloWorld.o"

you need to specify loc where you want to have this HelloWorld.o file(You should include all the files in one directory in my case it is calldll)


and this is the path of HelloWorld.c file

After this you now have a .o file


now you have to write HelloWorld.def file like this


where HelloWorld is the name of the class and _Hello is the native method name
save it in the same directory calldll

now you have to create a new dll HelloWorld.dll that will provide communication between java and other language

Use this command to generate a new dll

gcc -shared -o"C:\calldll\HelloWorld.dll" "C:\calldll\HelloWorld.o" "C:\HelloWorld\HelloWorld.def"

it will greate the new dll HelloWorld.dll overwrite the previous one


now you have done all steps

now open a new command prompt window

and compile and run the HelloWorld program
and C:\calldll>java HelloWorld

It will give you an output

Hello World!