Now we going to take a look about JNI (java native interface).
when you climb up to the top of the java , you will find out that you need to do some power full operations through the java , as a example you need to control your toy car trough java.
so there is no way to do this with pure java.(i think they disable this thing because if they do add that capability java lose the cross platform capability ) how ewer there was a norther way , its JNI.
JNI HelloWorld.
before you start tutorial install JDK & C++.
you can download them for free.
& set path for both compilers.
for JDK goto java install directory\java\jdk1.6.0_16\bin
as a example,
C:\Program Files\Java\jdk1.6.0_16\bin
copy this path.
now go to My Computer properties >> Advanced >>Enviroment Variables >>path >>Edit.
past your path after ";".
as a example,
other path;C:\Program Files\Java\jdk1.6.0_16\bin
do the same to c++ compiler,
as a example,
E:\Dev-Cpp\bin
after that it looks like this,
other path;C:\Program Files\Java\jdk1.6.0_16\bin;E:\Dev-Cpp\bin
then ok >> ok >> apply.
then write this program & compile it with java.
public class HelloWorld {
public native void Hello();
static {
System.load("C:/jana/HelloWorld.dll");
System.out.println("Loaded");
}
public static void main(String[] args) {
new HelloWorld().Hello();
}}
to compile use javac Helloworld.java
then dont type anything else like java HelloWorld or something ,if you do you will get error message.
after compile you will get HelloWorld.class file.
now we need to mack .h(header file).to this type this command.
javah -jni Helloworld
after that you will get HelloWorld.h file.
now type this to a txt and rename extension to .c.
//File: HelloWorld.c
#include <jni.h>
#include "HelloWorld.h"
#include <stdio.h>
JNIEXPORT void JNICALL Java_HelloWorld_Hello
(JNIEnv *env, jobject obj)
{
printf("Hello world!\n");
return;
}
to make .o file use this command,
gcc -c -I"<your jdk path>include" -I"<your jdk path>\include\win32" -o HelloWorld.o HelloWorld.c
ex:- gcc -c -I"C:\Program Files\Java\jdk1.6.0_16\include" -I"C:\Program Files\Java\jdk1.6.0_16\include\win32" -o HelloWorld.o HelloWorld.c
now we have .o file.
after that we need .def file.for do this simply add this code to txt and rename its extention to .def
EXPORTS
Java_HelloWorld_Hello
finally want to make a .dll file.for this enter this command,
gcc -shared -o HelloWorld.dll HelloWorld.o HelloWorld.def
now type java HelloWorld and you will see
Loaded
Hello world!