nxt's place

JNA: Java Native Access

by on Dec.09, 2007, under Java

If you use JNI to access native code you'll quickly find yourself writing some glue in c/c++ to access the native library. With JNA you can write nice clean code in Java without the need to glue your java code to the native library. This is all done by JNA.

All you need to do is define an interface that  (extends the Library interface and) exposes the methods in the library that you want to access:
import com.sun.jna.*;
// kernel32.dll uses the __stdcall calling convention
// Most C libraries will just extend com.sun.jna.Library
public interface Kernel32 extends StdCallLibrary {
// Method declarations, constant and structure definitions go here
}

Next you load the library using the following code:
Kernel32 kernel32Instance = (Kernel32)
    Native.loadLibrary("kernel32", Kernel32.class);

That's all there is to it, you can now access all methods you defined in the interface.
 Checkout the project page

:, , ,

Comments are closed.

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

Blogroll

A few highly recommended websites...