Wednesday, March 3, 2010

java JNI sample implementation

1. First create A simple java class.

public class HelloWorld {
public native void Hello();

static {
System.load("C:/test.dll");
System.out.println("Loaded");
}

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

2.

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

3.

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

4.

/ 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: Hello
Signature: ()V
/
JNIEXPORT void JNICALL Java_HelloWorld_Hello
(JNIEnv
, jobject);

#ifdef __cplusplus
}
#endif
#endif


5.

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
#include "HelloWorld.h"
#include
#include

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

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


5.


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
C:\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

C:\mingw\bin\>

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)

"C:\calldll\HelloWorld.c"

and this is the path of HelloWorld.c file

After this you now have a .o file

6.

now you have to write HelloWorld.def file like this

EXPORTS
Java_HelloWorld_Hello

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

7.

now you have done all steps

now open a new command prompt window

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


It will give you an output

Loaded
Hello World!

0 comments:

Post a Comment