Use JNI instead of JNA to call native code?

Viewed 50342

JNA seems a fair bit easier to use to call native code compared to JNI. In what cases would you use JNI over JNA?

10 Answers

Unless I'm missing something, isn't the main difference between JNA vs JNI that with JNA you can't call Java code from native (C) code?

In my specific application, JNI proved far easier to use. I needed to read and write continuous streams to and from a serial port -- and nothing else. Rather than try to learn the very involved infrastructure in JNA, I found it much easier to prototype the native interface in Windows with a special-purpose DLL that exported just six functions:

  1. DllMain (required to interface with Windows)
  2. OnLoad (just does an OutputDebugString so I can know when Java code attaches)
  3. OnUnload (ditto)
  4. Open (opens the port, starts read and write threads)
  5. QueueMessage (queues data for output by the write thread)
  6. GetMessage (waits for and returns data received by the read thread since the last call)
Related