JEP draft: Prepare to restrict the use of JNI
openjdk.org
openjdk.org
Non-Goals
There is no plan to remove JNI from the Java Platform.
You will be able to use JNI with a JVM flag set. Therefore no need to panic. Yet.If only where was a simple API that allowed us to get the handle...
On Java based platforms like Android, most likely never.
At least since ART became APEX compliant, Java 11 and 17 LTS are now available.
void Java_org_package_name_ClassName_method(JNIEnv *env, jobject _this, ...)
These are bound to `native` methods in classes when you load the library with `System.loadLibrary()`. Most often these libraries are thin wrappers that call into a C/C++/Rust library and convert data between Java and C representations (especially arrays and strings). It's tedious, it's annoying, and it's a platform-dependent artifact you need to ship with your app and build for all platforms on which your app runs.FFM, on the other hand, is a proper foreign function interface. You don't need any wrappers with it, you'd just load your native library of interest directly and obtain method handles to call its C functions.
Without going through C you don't have access to type definitions (you have to redeclare them in Java) or macros (sometimes not even impossible).
Structure layout might be different across platforms/versions/variants, symbols can be aliased through preprocessor etc. even though end API is the same.
This is a problem with any FFI that tries to avoid C. Look at this mess of trying to do POSIX in JNR: https://github.com/jnr/jnr-posix/tree/master/src/main/java/j... - each system needs different implementation and within them there is further branching based on versions. There is no guarantee that any of this will work in the future (except on Windows which maintains stable userland ABI).
This would seem extremely complicated to automate.
So it is a goal?