recipe № 021

Calling code from another source file (adapter pattern)

If you want to call method from the source code you have, there is nothing special here, when it comes to JNI. You can simply put it inside shared library that contains JNI functions. This is a typical sample of Adapter Pattern applied to Java calling native code. In this case, typically, you already have some sort of source code (written in C/C++/Fortran) and you want to make it available inside your Java code. In this case, JNI serves as adapter and allows to re-use already existing code base. Most common use case is very similar to picture below

In fact, there is not much to do from our side. All we have to do, is to make sure that we put all the sources inside single shared library. That’s all.

Let’s say we have this special function we want call.

and we have JNI code that is supposed to call it

All we have to do, is to put everything inside one, shared, library.

That’s all. You can find full sample here: recipe № 021.