Hey guys! How’s it going?
The #ForeignLinkerAPI of #JDK16 allows to invoke native code, typically DLLs or shared objects written in the C language, without neither a #JNI wrapper written in C, nor a third party library like #JNA.
Due to its cross-platform nature, Java never was a “good citizen” on any operating system. Besides the UI, which never really looked “native”, you mostly noticed this when it comes to calling native binary code. While you could write special libraries in C that fulfils the JNI contract, this is not what you actually wanted: Calling already existing THIRD PARTY libraries, like the Windows API for example. JNA and other libraries improved this, but were not as fast as JNI on one hand, and were not part of the official platform, so just replaced custom static glue code by third-party dynamic glue code. Still at runtime some JNI mumbo jumbo had to go on to copy data between the Java heap and the native heap, which is rather slow. And the worst thing is, JNA was only available for a small set of platforms, WAY less than the lots of platforms Java runs on!
With JDK 16, the Foreign Linker API (and its twin, the Foreign Memory Access API) enters the stage, and this will be a real game changer! While it still is in incubation, which mean it will definitively change over the next few JDKs before it will be stable, it already shows its powers: It is deeply built into the Java runtime itself, and it natively links the loaded native binary code with the Java runtime itself. Just like keeping the Java interface, but providing the actual native code as the implementation. Due to that, it is at least as fast as JNI (possibly fast as potentially less data is moved or converted between the OS memory and the JVM memory), can load ANY third party code, hence does not need a custom DLL to be written by you in C, nor does it need a third party glue code like JNA. Data binding works directly with the native heap using the new Foreign Memory Access API, so it prevents the expensive Java code mirroring unless actually needed. As the whole glue code is written in Java, and both APIs are part of the JRE, this API works on ALL platforms without any third party stuff or glue code in C!
Ain’t that f*ing great?! Guys, I waited for this since decades! Yes, JNA was ok, and I even contributed to it. But the foreign linker API and foreign memory access APIs is what we all REALLY want. It just feels so good! Try it out AT ONCE!
If you like this video, please give it a thumbs up, share it, subscribe to my channel, or become my patreon. Thanks!
Stay safe and… Party on!