Title: Add "Calling out to C code from Java" pattern: JNI vs FFM API by Copilot · Pull Request #130 · javaevolved/javaevolved.github.io · GitHub
Open Graph Title: Add "Calling out to C code from Java" pattern: JNI vs FFM API by Copilot · Pull Request #130 · javaevolved/javaevolved.github.io
X Title: Add "Calling out to C code from Java" pattern: JNI vs FFM API by Copilot · Pull Request #130 · javaevolved/javaevolved.github.io
Description: Adds a new advanced language pattern contrasting the legacy JNI approach with the modern Foreign Function & Memory (FFM) API introduced in Java 22, highlighting how FFM eliminates the need for C-side Java awareness entirely.
New pattern: content/language/call-c-from-java.yaml
Both the old and modern sides call strlen("Bambi") and print "Return value 5", making them directly comparable:
Old (JNI): declare native long strlen(String s) → run javac -h → write a C wrapper using JNI conventions (JNIEnv*, GetStringUTFChars, ReleaseStringUTFChars, etc.) → compile and link a .so
Modern (FFM): call strlen directly from the C stdlib via Linker, Arena, and MethodHandle — no C code required at all
The comparison highlights that JNI can't even call a simple stdlib function without a C wrapper, while FFM calls it directly:
void main() throws Throwable {
try (var arena = Arena.ofConfined()) {
var stdlib = Linker.nativeLinker().defaultLookup();
var foreignFuncAddr = stdlib.find("strlen").orElseThrow();
var strlenSig =
FunctionDescriptor.of(ValueLayout.JAVA_LONG, ValueLayout.ADDRESS);
var strlenMethod =
Linker.nativeLinker().downcallHandle(foreignFuncAddr, strlenSig);
var ret = (long) strlenMethod.invokeExact(arena.allocateFrom("Bambi"));
IO.println("Return value " + ret); // 5
}
}
Navigation chain
compact-canonical-constructor → call-c-from-java → enterprise/servlet-vs-jaxrs
Proof script: proof/language/CallCFromJava.java
Calls strlen via FFM; uses //JAVA_OPTIONS --enable-native-access=ALL-UNNAMED
Translations
Added for all 12 non-English locales: de, es, pt-BR, zh-CN, ar, fr, ja, ko, bn, it, pl, tr
Technical terms (JNI, FFM, javac, JNIEnv, MethodHandle, Arena, JDK 22) are preserved untranslated in all locales
Original prompt
This section details on the original issue you should resolve
Open Graph Description: Adds a new advanced language pattern contrasting the legacy JNI approach with the modern Foreign Function & Memory (FFM) API introduced in Java 22, highlighting how FFM eliminates the need for ...
X Description: Adds a new advanced language pattern contrasting the legacy JNI approach with the modern Foreign Function & Memory (FFM) API introduced in Java 22, highlighting how FFM eliminates the need ...
Opengraph URL: https://github.com/javaevolved/javaevolved.github.io/pull/130
X: @github
Domain: github.com
| route-pattern | /:user_id/:repository/pull/:id/files(.:format) |
| route-controller | pull_requests |
| route-action | files |
| fetch-nonce | v2:1286494a-9f64-c0ca-178d-68a3c98142c5 |
| current-catalog-service-hash | ae870bc5e265a340912cde392f23dad3671a0a881730ffdadd82f2f57d81641b |
| request-id | C4B2:21ABD1:1C69E3E:26C7CD6:6A6089B5 |
| html-safe-nonce | 8be44257e58c013c1bbdffa5dcf185dfe4384109bd793b334e01a6024da6b000 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDNEIyOjIxQUJEMToxQzY5RTNFOjI2QzdDRDY6NkE2MDg5QjUiLCJ2aXNpdG9yX2lkIjoiMjA4MTI3ODc5NDc5NjE0MDk4MiIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | c234892134fed4e34978bd65f2986006e5c9d30d8d0e35a026a1f625ee576773 |
| hovercard-subject-tag | pull_request:3346498695 |
| github-keyboard-shortcuts | repository,pull-request-list,pull-request-conversation,pull-request-files-changed,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/javaevolved/javaevolved.github.io/pull/130/files |
| twitter:image | https://avatars.githubusercontent.com/in/1143301?s=400&v=4 |
| twitter:card | summary_large_image |
| og:image | https://avatars.githubusercontent.com/in/1143301?s=400&v=4 |
| og:image:alt | Adds a new advanced language pattern contrasting the legacy JNI approach with the modern Foreign Function & Memory (FFM) API introduced in Java 22, highlighting how FFM eliminates the need for ... |
| og:site_name | GitHub |
| og:type | object |
| hostname | github.com |
| expected-hostname | github.com |
| None | c0632c7929f76e8db538ffed06ab2fc4c524a012b0888e95d96928690fcc604d |
| turbo-cache-control | no-preview |
| diff-view | unified |
| go-import | github.com/javaevolved/javaevolved.github.io git https://github.com/javaevolved/javaevolved.github.io.git |
| octolytics-dimension-user_id | 261976795 |
| octolytics-dimension-user_login | javaevolved |
| octolytics-dimension-repository_id | 1159421298 |
| octolytics-dimension-repository_nwo | javaevolved/javaevolved.github.io |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 1159421298 |
| octolytics-dimension-repository_network_root_nwo | javaevolved/javaevolved.github.io |
| turbo-body-classes | logged-out env-production page-responsive full-width |
| disable-turbo | true |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 4f9009410bd92637ba907f71dc7f7af3f0df45aa |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width