# Java Native Access
# Introduction to JNA
# What is JNA?
Java Native Access (JNA) is a community-developed library providing Java programs an easy access to native shared libraries (.dll
files on windows, .so
files on Unix ...)
# How can I use it?
For the purpose of this introduction, we suppose the native platform in use is Windows. If you're running on another platform simply replace the string "msvcrt"
with the string "c"
in the code below.
The small Java program below will print a message on the console by calling the C (opens new window) printf
function.
CRuntimeLibrary.java
package jna.introduction;
import com.sun.jna.Library;
import com.sun.jna.Native;
// We declare the printf function we need and the library containing it (msvcrt)...
public interface CRuntimeLibrary extends Library {
CRuntimeLibrary INSTANCE =
(CRuntimeLibrary) Native.loadLibrary("msvcrt", CRuntimeLibrary.class);
void printf(String format, Object... args);
}
MyFirstJNAProgram.java
package jna.introduction;
// Now we call the printf function...
public class MyFirstJNAProgram {
public static void main(String args[]) {
CRuntimeLibrary.INSTANCE.printf("Hello World from JNA !");
}
}
# Where to go now?
Jump into another topic here or jump to the official site (opens new window).