Quick revision
The short answer
JNI required hand-written C glue for every native function and made native code hard to review and use safely, so the JDK added the Foreign Function and Memory API, which calls native code from Java with little glue and is now the recommended way. As part of making integrity the default, recent JDKs warn when code uses JNI or restricted FFM methods without --enable-native-access. For security, this means we should list which modules or JARs may access native code, grant that access explicitly, and treat native libraries as high-risk dependencies with their own patching and review.
What to remember
In this answer, we saw that JNI needed hand-written C glue and gave any code the power to run native code unchecked, so the JDK added the FFM API as the recommended way to call native code and, as part of integrity by default, now warns when JNI or restricted FFM methods are used without --enable-native-access. For security, we inventory native access, grant it explicitly and narrowly by module, and treat native libraries as high-risk dependencies with their own patching, review and isolation.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
JNI required hand-written C glue for every native function and made native code hard to review and use safely, so the JDK added the Foreign Function and Memory API, which calls native code from Java with little glue and is now the recommended way. As part of making integrity the default, recent JDKs warn when code uses JNI or restricted FFM methods without --enable-native-access. For security, this means we should list which modules or JARs may access native code, grant that access explicitly, and treat native libraries as high-risk dependencies with their own patching and review.
Why this question is asked
For most of Java's history, any library on the class path could load a native library and run arbitrary machine code inside our process, and nobody had to approve it. The JDK is changing that, and the change affects how we build, deploy and review applications. Interviewers ask this at leadership level to see whether we understand the reasons behind the platform's direction and can turn them into security policy. In this answer, we will look at the problems with JNI, what the FFM API changes, what integrity by default means and how native access is restricted, and then what the change means for our security posture.
1What Was Wrong With JNI
JNI, the Java Native Interface, has been in Java since 1.1. It lets Java call C code, but only through a C wrapper written by hand for each function, following JNI's naming and type conventions. That glue code is where many bugs live: wrong reference handling, missed exception checks, buffer overflows. It is also hard to review, because the reviewers of a Java service are rarely C experts, and the code is often hidden inside a JAR as a prebuilt binary.
Most importantly, JNI was unrestricted. Any code that could call System.loadLibrary could load a native library and run anything in the JVM's process, outside every Java safety guarantee. A dependency three levels deep could do it without the application's authors knowing. And once native code runs, it can break the guarantees the rest of the platform relies on, for example by writing into the memory of objects, including fields declared final.
2What the FFM API Changes
The Foreign Function and Memory API, final in Java 22, calls native functions directly from Java. We describe a C function's signature with a FunctionDescriptor, the Linker gives us a MethodHandle, and we call it. Native memory is accessed through MemorySegment, which is bounds-checked and tied to an Arena that controls its lifetime. Tools such as jextract generate the bindings from C headers. So there is far less hand-written C, and the remaining risk is concentrated in the native library itself rather than spread across glue code.
That makes FFM the recommended way to reach native code in new work. But it is still native code: a bad pointer passed to a C function can still crash the process. So the FFM API marks some of its methods as restricted, the ones that can break integrity, such as creating a downcall handle or reinterpreting a segment's size, and these are subject to the same access rules as JNI.
| Aspect | JNI | FFM API |
|---|---|---|
| Glue code | Hand-written C per function | Java only; jextract generates bindings |
| Memory access | Raw pointers, no checks | Bounds and lifetime checks on segments |
| Review | Needs C expertise | Mostly Java, native risk in the library |
| Access control | Warns without grant (recent JDKs) | Restricted methods warn without grant |
| Status | Supported, legacy | Final in 22; recommended |
3Integrity by Default and Restricted Native Access
The JDK has been working for several years toward integrity by default: the guarantees of the Java language and platform, such as encapsulation and immutability of final fields, should hold unless the application explicitly chooses to give them up. Strong encapsulation of JDK internals in Java 16 and 17 was one step, the deprecation of sun.misc.Unsafe memory access methods another, and restricting native access is the next.
Since Java 22, calling a restricted FFM method prints a warning unless native access is enabled for the calling module. Since Java 24, the same applies to JNI: loading a native library or linking a native method from code without native access enabled prints a warning. The access is granted with the --enable-native-access option, naming modules or ALL-UNNAMED for the class path, or with the Enable-Native-Access manifest attribute in an executable JAR. A related option, --illegal-native-access, controls what happens otherwise, and the stated direction is that a future release will deny it by default; check your version for the current default.
Notice that the granularity is the module. On the class path, everything is in one unnamed module, so the grant is all or nothing. That is a good reason to move the code that needs native access into a named module, so the grant covers only that code.
4What It Means for Our Security Posture
The new rules give us something we never had: a visible list of which code may run native code. We should use it. First, we inventory native access: run every service on a recent JDK, collect the warnings, and record which libraries load native code and why. Usually the list is short: a compression library, a database driver, a crypto provider, an image library.
Second, we grant access explicitly and narrowly, in the service's startup configuration or manifest, named by module where possible, and reviewed like any other permission. A new library that suddenly needs native access then shows up as a warning in CI and requires a decision, instead of silently gaining the power to run machine code in production.
Third, we treat native libraries as high-risk dependencies. They get the same scrutiny as a new network-facing component: who builds the binary, how quickly security fixes arrive, which platforms are covered. Their versions are pinned, their CVEs tracked separately, because the dependency scanners that understand Maven coordinates may not see a C library bundled inside a JAR. Where the native code processes untrusted input, such as images or documents from users, we isolate it in a separate process or service.
- Run on a recent JDK in CIcollect native access warnings
- Is each native user known and approved?YesGrant --enable-native-access for that moduleNoReview the library or remove it
- Does it handle untrusted input?YesIsolate in a separate process or serviceNoKeep in process, pinned and tracked
- Fail the build on new unapproved native accesstreat warnings as findings
4 steps in order. Play them, or step through them one at a time.
5Follow-up questions
Will JNI be removed?
There is no plan to remove JNI; a huge amount of existing code depends on it. The change is that using it becomes an explicit, granted choice, and new work is encouraged to use the FFM API.
Why grant per module instead of per library?
The module is the unit the JDK can reliably identify as the caller. Granting per module keeps the permission close to the code that needs it; on the class path everything is one unnamed module, which is why moving native-using code into a named module gives a tighter grant.
Does this replace the Security Manager?
No. The Security Manager was a general permission system and has been permanently disabled in Java 24. Integrity by default is narrower: it makes specific powerful operations, such as native access and deep reflection, explicit choices of the application rather than something any library can do.
6Conclusion
In this answer, we saw that JNI needed hand-written C glue and gave any code the power to run native code unchecked, so the JDK added the FFM API as the recommended way to call native code and, as part of integrity by default, now warns when JNI or restricted FFM methods are used without --enable-native-access. For security, we inventory native access, grant it explicitly and narrowly by module, and treat native libraries as high-risk dependencies with their own patching, review and isolation.
Open with
JNI worked, but it needed hand-written C glue for every function, which was error-prone and hard for Java teams to review, and it was completely unrestricted: any library on the class path could load native code and run anything inside our process, even break guarantees like final fields.
Then, if they want more
The JDK's answer has two parts. The Foreign Function and Memory API, final in Java 22, lets us call native functions from Java with little glue, bounds-checked memory and arena lifetimes, so it is now the recommended way.
And under integrity by default, native access becomes an explicit choice: restricted FFM methods warn since 22, JNI warns since 24, unless the module is granted with --enable-native-access or the Enable-Native-Access manifest attribute, and a future release is expected to deny it by default. For our security posture I would use this as a control.
Inventory which modules and JARs load native code by collecting those warnings in CI, grant access explicitly and narrowly, preferably to a named module, and fail the build on new unapproved native access. And treat native libraries as high-risk dependencies, pinned, separately tracked for CVEs, and isolated in another process when they handle untrusted input.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 22, Java 24, Java 16
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- The Java Language Specification, Java SE 21, Java SE 21 API documentation
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "JNI has been in Java since 1.1, yet the platform now restricts native access and added a...": https://scrivio-interview-library.netlify.app/java/foreign-memory-and-native/6dd7b19f3b/