Jump to content

Java Native Access

From Wikipedia, the free encyclopedia

This is an old revision of this page, as edited by 195.47.234.106 (talk) at 11:40, 2 May 2009 (→‎Example). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

Java Native Access
Stable release
3.0.9 / October 30, 2008 (2008-10-30)
Repository
Written inC and Java
Operating systemCross-platform
Size606.9 KB (archived)
TypeSoftware Library
LicenseLGPL version 2.1 or later.
Websitehttps://jna.dev.java.net

Java Native Access provides Java programs easy access to native shared libraries without using the Java Native Interface. JNA's design aims to provide native access in a natural way with a minimum of effort. No boilerplate or generated glue code is required. While some attention is paid to performance, correctness and ease of use take priority. The JNA library uses a small native library called libffi to dynamically invoke native code. The JNA library uses native functions that allow code to load a library by name and retrieve a pointer to a function within that library, and uses libffi library to invoke it, all without static bindings, header files, or any compile phase. The developer uses a Java interface to describe functions and structures in the target native library. This makes it quite easy to take advantage of native platform features without incurring the high overhead of configuring and building JNI code.

Adoption

Java Native Access is known to be used in:

Mapping types

The following table shows the mapping of types between Java and native code and supported by the JNA library.

Native Type Size Java Language Type Common Windows Types
char 8-bit integer byte BYTE, TCHAR
short 16-bit short short WORD
wchar_t 16/32-bit character char TCHAR
int 32-bit integer int DWORD
int boolean value boolean BOOL
long 32/64-bit integer NativeLong LONG
long long, __int64 64-bit integer long
float 32-bit FP float
double 64-bit FP double
char* C string String LPTCSTR
void* pointer Pointer LPVOID, HANDLE, LPXXX

Example

The following program loads the local C standard library implementation and uses it to call the printf function.

Note: The following code is portable and works the same on Windows and Linux / Unix / Mac OS X platforms.

import com.sun.jna.Library;
import com.sun.jna.Native;
import com.sun.jna.Platform;

/** Simple example of native library declaration and usage. */
public class HelloWorld {
   public interface CLibrary extends Library {
     CLibrary INSTANCE = (CLibrary) Native.loadLibrary(
       (Platform.isWindows() ? "msvcrt" : "c"), CLibrary.class);
           void printf(String format, Object... args);
	}

    public static void main(String[] args) {
     CLibrary.INSTANCE.printf("Hello, World\n");
        for (int i = 0; i < args.length; i++) {
           CLibrary.INSTANCE.printf("Argument %d: %s\n", i, args[i]);
        }
      }
}

The following program loads the C POSIX library and uses it to call the standard mkdir function.

Note: The following code is portable and works the same on POSIX standards platforms.

import com.sun.jna.Library;
import com.sun.jna.Native;

/** Simple example of native C POSIX library declaration and usage. */
public class exampleOfPOSIX {
   public interface POSIX extends Library {
	public int chmod(String filename, int mode);
	public int chown(String filename, int user, int group);
	public int rename(String oldpath, String newpath);
	public int kill(int pid, int signal);
	public int link(String oldpath, String newpath);
	public int mkdir(String path, int mode);
	public int rmdir(String path);
   }

   public static void main(String[] args) {
	POSIX posix = (POSIX) Native.loadLibrary("c", POSIX.class);
	posix.mkdir("/tmp/newdir", 777);
	posix.rename("/tmp/newdir","/tmp/renamedir");
   }
}

The program below loads the Kernel32.dll and uses it to call the Beep and Sleep functions.

Note: The following code works only on Windows platforms.

import com.sun.jna.Library;
import com.sun.jna.Native;

/** Simple example of Windows native library declaration and usage. */
public class BeepExample {
   public interface Kernel32 extends Library {
       // FREQUENCY is expressed in hertz and ranges from 37 to 32767
       // DURATION is expressed in milliseconds
       public boolean Beep(int FREQUENCY, int DURATION);
       public void Sleep(int DURATION);
   }

   public static void main(String[] args) {
	Kernel32 lib = (Kernel32) Native.loadLibrary("kernel32", 
           Kernel32.class);
	lib.Beep(698, 500);
	lib.Sleep(500);
	lib.Beep(698, 500);
   }
}

References

See also