JNA Tutorial To Load C/C++ DLL with Java Sample Program

Hello Dear Readers, today we are going to tell you about JNA (Java Native Access) concept used in Java applications. In this JNA tutorial, you would find useful coding tips to call “C” Library Functions or API using JNA from Java code.

You can apply JNA concept in various applications like a test automation framework where you can use JNA to integrate your C code with the automation framework.

And if you are creating web browser extensions where you need to interact with the file system, then you can use JNA to call system Apis to meet your requirements.

There is an alternative method known as JNI which can also load a C/C++ DLL. It’s also quite popular but requires writing additional Java wrapper code to handle data types and methods of the underlying C/C++ library.

We’ll cover the JNI method at length sometime later. For now, let’s focus on learning JNA concept and its application.

JNA Tutorial – What is JNA (Java Native Access)?

JNA Tutorial with Java Sample Program.

JNA Tutorial with Java Sample Program.


In this JNA tutorial, we’ll give you a basic idea of JNA via a code sample. You can directly reuse the code of this JNA example in your project.

Since JNA is platform independent, so it supports multiple platforms and the following C library types.

1- DLL (Dynamic Link Library) on Windows platforms.

2- SO (Shared Object) on Linux platforms.

JNA, if you compare it with JNI is far easier to use and implement. It will step-up your coding speed if you are working on Java project making use of C Libraries.

Creating a sample JNA project.

Let’s see how does it work? The first step for you is to download and import JNA (Java Native Access) to your project and write a simple class.

Next, create a simple Java program which uses two Java class files as mentioned below-

1- The first file is interface file which contains the definition of the JNA functions defined in “C” library.

package JNAApiInterface;
import com.sun.jna.Library;

public interface JNAApiInterface extends Library {
    JNAApiInterface INSTANCE = (JNAApiInterface) Native.loadLibrary((Platform.isWindows() ? "msvcrt" : "c"), JNAApiInterface.class);
    void printf(String format, Object... args);
    int sprintf(byte[] buffer, String format, Object... args);
    int scanf(String format, Object... args);


2- The second Java file is the implementation file which imports the Interface class and calls its API for validation.

package JNABucket;
import JNAApiInterface;
import com.sun.jna.Native;

public class JNABucket {
    public static void main(String args[]) {
        JNAApiInterface jnaLib = JNAApiInterface.INSTANCE;
        jnaLib.printf("Hello World");
        String testName = null;

        for (int i = 0; i < args.length; i++) {
            jnaLib.printf("\nArgument %d : %s", i, args[i]);

        jnaLib.printf("\Please Enter Your Name:\n");
        jnaLib.scanf("%s", testName);
        jnaLib.printf("\nYour name is %s", testName);



We hope that this JNA tutorial would have given you the desired solution to your problem. If it did, then please do leave us your feedback in the comment section.

Your inputs will encourage our team to deliver better content every time we decide to post.

In the end, if you find this JNA tutorial useful, then don’t miss to share this post with your friends and to the social media of your choice.





  1. Meenakshi Agarwal
  2. Manju Gupta
    • Harsh S.