JNA, using native code with Java, demo

To use native code with Java, JNA is simpler than JNI, here is a simple demonstration...

Java Native Access is an extension to Java that allows the use of native APIs by including dynamically library files, DLLs under Windows.
Unlike JNI, it does not require to generate code for using C functions. To use them, simply include the file that defines them and declare the header of these functions in an interface.

We want to use the puts function of the C language, which is provided by the msvcrt.dll file in Windows, create the following interface:

package CInterface; 
       
import com.sun.jna.Library; 
       
public interface CInterface extends Library 
{ 
      public int puts(String str);
}     

We have declared the CInterface interface which is a subclass of JNA.
Inside the interface, C functions are declared as methods. To use the method puts simply create an instance of CInterface:

 CInterface demo = (CInterface) Native.loadLibrary(libName, CInterface.class); 
 demo.puts("Hello World!");

For this to work, some imports are required:

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

In addition, the library jna.jar must be included in the project.

A complete project for NetBeans can be downloaded. It contains two sources:
- CInterface.java that holds the interface above.
- Hello.java that uses this interface.

To run the demonstration:

  1. Download and install NetBeans.
  2. Download the project file, it is unpacked into the JNA directory.
  3. Download the jna.jar library on Sun's site and copy it into the JNA directory.
  4. Load the project in NetBeans from this directory.
  5. Add jna.jar to the list of libraries:
    Project Properties -> Libraries -> Add JAR / Folder.
  6. Compile the project.

Then type the command:

java -jar /jna/dist/hello.jar "My own text!"

You can add the interface to all the functions you need, if they are present in the same DLL, and create an interface for each DLL file to include.

Full source code:

// JNA Demo. Scriptol.com
package CInterface;
import com.sun.jna.Library;
import com.sun.jna.Native;
import com.sun.jna.Platform;
public class hello
{
public static void main(String[] args)
{
String mytext = "Hello World!";
if (args.length != 1)
{
System.err.println("You can enter you own text between quotes...");
System.err.println("Syntax: java -jar /jna/dist/demo.jar \"myowntext\"");
}
else
mytext = args[0];
// Library is c for unix and msvcrt for windows
String libName = "c";
if (System.getProperty("os.name").contains("Windows"))
{
libName = "msvcrt";
}
// Loading dynamically the library
CInterface demo = (CInterface) Native.loadLibrary(libName, CInterface.class);
demo.puts(mytext);
}
}

Download the source code.

The archive holds the file of the demo which displays a simple text using the puts function of the C language.
A project file for NetBeans is included.

See the Github project of Java Native Access for the jna.jar library.