Title: [Pattern] · Issue #129 · javaevolved/javaevolved.github.io · GitHub
Open Graph Title: [Pattern] · Issue #129 · javaevolved/javaevolved.github.io
X Title: [Pattern] · Issue #129 · javaevolved/javaevolved.github.io
Description: Category language Slug call-c-from-java Title Calling out to C code from Java Difficulty advanced Since JDK 22 Summary Simplifies C/C++ code to be called from Java, allowing most existing libraries to be used without adapters or cumberso...
Open Graph Description: Category language Slug call-c-from-java Title Calling out to C code from Java Difficulty advanced Since JDK 22 Summary Simplifies C/C++ code to be called from Java, allowing most existing libraries...
X Description: Category language Slug call-c-from-java Title Calling out to C code from Java Difficulty advanced Since JDK 22 Summary Simplifies C/C++ code to be called from Java, allowing most existing libraries...
Opengraph URL: https://github.com/javaevolved/javaevolved.github.io/issues/129
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"[Pattern]","articleBody":"### Category\n\nlanguage\n\n### Slug\n\ncall-c-from-java\n\n### Title\n\nCalling out to C code from Java\n\n### Difficulty\n\nadvanced\n\n### Since JDK\n\n22\n\n### Summary\n\nSimplifies C/C++ code to be called from Java, allowing most existing libraries to be used without adapters or cumbersome C code modification.\n\n### Old code label\n\nJava 1.1+\n\n### Old code\n\n```java\npublic class CallCFromJava {\n\tstatic { System.loadLibrary(\"hello-jni\"); }\n\tpublic static native int greet(String name);\n\tpublic static void main(String[] args) {\n\t\tint ret = greet(\"Bambi\");\n\t\tSystem.out.println(\"Return value \" + ret);\n\t}\n}\n\nThen run javac -h, then view the generated .h file, then write your .c file:\n\n#include \"CallCFromJava.h\"\n\nJNIEXPORT int JNICALL Java_CallCFromJava_greet(JNIEnv *env, jclass this, jstring str) {\n\tconst char* name = (*env)-\u003eGetStringUTFChars(env, str, NULL);\n\tprintf(\"Hello %s\\n\", name);\n\treturn 0;\n}\n```\n\n### Modern code label\n\nJava 22+\n\n### Modern code\n\n```java\npublic class CallCFromJava {\n void main() throws Throwable {\n\n try (Arena arena = Arena.ofConfined()) {\n SymbolLookup ourLib = \n SymbolLookup.libraryLookup(\"libhello-ffi.so\", arena);\n Optional\u003cMemorySegment\u003e segment = ourLib.find(\"greet\");\n MemorySegment foreignFuncAddr = segment.get();\n FunctionDescriptor greet_sig= // return and input parameter\n FunctionDescriptor.of(ValueLayout.JAVA_LONG, ValueLayout.ADDRESS);\n MethodHandle greetMethod =\n Linker.nativeLinker().downcallHandle(foreignFuncAddr, greet_sig);\n var ret = (long) greetMethod.invokeExact(arena.allocateFrom(\"Bambi\"));\n System.out.println(\"Return value \" + ret);\n }\n }\n}\n\nC Code:\nint greet(char* name) {\n printf(\"Hello %s\\n\", name);\n return 0;\n}\n```\n\n### Explanation\n\nJava has two techniques for interfacing between Java and C/C++ code, the traditional JNI and the newer FFM. With JNI you simply declare your method as native, but then you have to run `javac -h` to generate a C-language .h (header) file, then have your C code implement that, and deal with the slightly cumbersome JNI C-language API. \n\nFFM simplifies the C code to the point that it doesn't have to know anything about Java. This makes it *much* easier to interface with existing C/C++-language libraries. It does take a few more lines of Java code, but the net effect is more readable and more maintainable. Compare the C code in both versions and you'll see the difference!\n\nMinimal runnable demo can be found at https://github.com/IanDarwin/javaevolved-java_callling_c \n\n### Why the modern way wins\n\n👁 Better readability — C code doesn't have to learn Java!\n⚡ More flexible - can directly call most existing C/C++ libraries\n⚡ Easier - don't have to stop, run javac -h, and implement the \"interface\" defined in .h file.","author":{"url":"https://github.com/IanDarwin","@type":"Person","name":"IanDarwin"},"datePublished":"2026-03-02T14:15:02.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":1},"url":"https://github.com/129/javaevolved.github.io/issues/129"}
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:0b2cae82-a979-c9dc-34c0-127fc072d193 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | E43E:8F66F:295A31:38FFA1:6A602746 |
| html-safe-nonce | b51d595a22f471d5c60169d8943f258c3c4388cdc5473cc7c71132be90fef6ed |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFNDNFOjhGNjZGOjI5NUEzMTozOEZGQTE6NkE2MDI3NDYiLCJ2aXNpdG9yX2lkIjoiNzY5ODIwNjc2NjQ2MDMxNTQ2MiIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | aa2f248e11b48ed0e068153ac6f78d53297178035305ebe3e5eef2fdf42a28f6 |
| hovercard-subject-tag | issue:4011516185 |
| github-keyboard-shortcuts | repository,issues,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/_view_fragments/issues/show/javaevolved/javaevolved.github.io/129/issue_layout |
| twitter:image | https://opengraph.githubassets.com/fbaf5e5968dd216ce6b6e25eb7a6929c84dfc721d237b174221810b96cd27fdc/javaevolved/javaevolved.github.io/issues/129 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/fbaf5e5968dd216ce6b6e25eb7a6929c84dfc721d237b174221810b96cd27fdc/javaevolved/javaevolved.github.io/issues/129 |
| og:image:alt | Category language Slug call-c-from-java Title Calling out to C code from Java Difficulty advanced Since JDK 22 Summary Simplifies C/C++ code to be called from Java, allowing most existing libraries... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | IanDarwin |
| hostname | github.com |
| expected-hostname | github.com |
| None | 60da8c2a42fa2bbf5f7567474990ec467836a84444262a58e200fa91b7f3d2d0 |
| turbo-cache-control | no-preview |
| 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 |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 9824515e740d83d5eb82168a089b806ab0fe04a1 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width