JNI allows us to write some code in C/C++ and then talk to that code through Java. In part one of this two part article, Neville introduces us to the concepts behind JNI with a simple example in C.
Interacting With C From Java With JNI: Part 1/2 (Page 1 of 3 )
Platform independence is one of the most common buzzwords in the industry today. Everybody wants flexibility. Everybody wants to program in his or her favorite programming language. Sometimes of course it's not a matter of choice, as some components of an application have to be programmed in a completely different language for whatsoever reason. Well, in Java we have JNI as a solution. Let me tell you in short what we can do using the JNI. Basically we can write code in other native programming languages (like C and C++) and use that same code in our java file.
JNI has a concept called native methods. We would have to declare all of the methods (which we would be coding) in other programming language as native. So let's get this concept straight once more before we proceed: JNI is the interface between the native programming language and the Java file (its like saying it's the technology that makes this whole thing work) and native methods are those methods which we will be coding in their native programming language (such as C or C++). The JNI and native methods collectively help us attain the flexibility in coding which we are looking for.
Again, like most of my articles, this includes practical examples so hopefully if all goes well, you would have a working example of Java interacting with C using the JNI.
Of course before you read on I will assume that you know the basics of C and Java. You will also need Java and C compilers, which are readily available. We will be using two C header files called jni.h and jni_md.h, which are present in the C:\jdk\include and C:\jdk\include\win32 folders respectively. Just copy and paste them in your current working folder for the time being so that we can include them in our C code.