Foreign Linker API: Java native access without C | Modern Java | JDK16 | Head Crashing Informatics 27

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!

About Markus Karg

Java Guru with +30 years of experience in professional software development. I travelled the whole world of IT, starting from Sinclair's great ZX Spectrum 48K, Commodore's 4040, over S/370, PCs since legendary XT, CP/M, VM/ESA, DOS, Windows (remember 3.1?), OS/2 WARP, Linux to Android and iOS... and still coding is my passion, and Java is my favourite drug!
This entry was posted in Jakarta EE, Java, Programming, Standards and tagged , , , , , , , , , , , , . Bookmark the permalink.