Hello everyone! This week I was trying to interface my USB device with my computer. I was able to do some C programming to do my job. However I wanted to use java in addition to make it look cool with some very easy (but not so good) java Swing. So I thought of doing some research on JNI (Java Native Interface )and started my coding and the result was quite fascinating. The speed of execution was almost same and I didn’t have any problem with my USB device speed as well ( although speed was not the major concern ) . So I wanted to share some of my experience.
So What Is JNI ?
Java Native Interface(JNI) is native programming interface for java programmers to use code/libraries which has been written in other programming language like C and C++ . In this tutorial we will use some C code to do the job.
Is JNI necessary ?
Well it depends on what you are trying to achieve. Pure java based code is enough in almost all the applications but some times you will be concerned about memory management and some time crical operatoion and even some operation which require OS root access which cannot be achieved from java code. Let us say that you want to use RAW sockets in you application. Java doesnot support raw sockets and hence you will have to go for C/C++.So it depends on what you are trying to achieve.
What will this tutorial cover ?
In this tutorial we will know how to call C/C++ function from java. We will also learn to get the results from C/C++ computation. Also we will learn to make callback functions inorder to call java functions from C/C++ code. Click here to continue.