Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Why would I use it over JNI? I used JNI few times and it was very easy and straightforward.

Can I use it to call any WinAPI without C glue?



JNI => "I write C code & after compilation java can access it as com.package.ClassName"

JNA => "I write java code & load libsomething at runtime, asking for FuncName(int, int) which java can then call into native code."

You want JNA for your use case.


So what is the difference between this and JNA? I assume it's not the same since it's taking a while to be added to the language


JNI is the only escape hatch in the JRE. JNA leverages JNI and so has a better API while still keeping the terrible performance. This is like JNA, but more robust, and while also being an escape hatch of its own so that you can finally get performance similar to P/Invoke when making native calls.


This is apparently focused on efficiently accessing off-heap memory. In Jni lot of things require copies. Only JNI API that's reliably copy-free is ByteBuffer API.


JNI requires you to write your own C code to call into. I generally want to do that as little as possible, for many reasons (I’d have to build it and ship it myself.)


JNI is extremely tedious. Not only do you have to have a native library built for every platform you support, you also have to manually convert between Java and C representations of things. A lot of glue code for which there are no generators (I think). JNA is better, it still relies on JNI, but uses a tiny stub library to allow you to link to and call arbitrary functions from C dynamic libraries in pure Java.

This feature is basically JNA, but more efficient and built into the JVM itself.


Read under `Motivation`, I think it answers your question. Of course you can still use JNI.




Consider applying for YC's Summer 2026 batch! Applications are open till May 4

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: