News for hackers. Stuff that matters.
Re:How to Use the Foreign Function API in Java 22 to Call C ... by jakjak123 on Thursday May 09, 2024 @05:00AM (#40306428)
I had a C library I needed to ideally use from Java directly. The new FFI API looks great, but unfortunately the C API relied heavily on macros and void* arguments, making it incredibly difficult to model from Java.
Re:How to Use the Foreign Function API in Java 22 to Call C ... by the-alchemist on Thursday May 09, 2024 @09:40AM (#40308136)
I would give the jextract tool a try. I believe it uses LLVM to parse the header files, so the generated bindings might actually be pretty good.
Re:How to Use the Foreign Function API in Java 22 to Call C ... by jakjak123 on Sunday May 12, 2024 @08:59AM (#40334075)
I did use jextract for java 19+20, but it looked very messy.

Tried it again yesterday on java 22, and the helpers from jextract are waaaay better. I actually completed a MVP implementation this time in a couple of hours. This could perhaps be released as library if I find the effort to wrap it in a meaningful way!

We currently wrap this in java by calling the binary with subprocesses, which has been working great at some latency overhead. The big bonus of this though, is that we can kill the process from java when it misbehaves. Putting this C code inside Java again, means we likely lose that control.