How to call native code from a Java application

This post explain different methods to call the operating system commands or native code from a Java application.

Methods to call a native function

Method 1. Use Runtime class

Runtime class contains the exec method that allows the application to run an operating system command.

An example of how to use this method:

String command = "echo \"Hello World!\"";

Method 2. Use JNI

Java Native Inteface (JNI) is a foreign function interface that allows code running on JVM to call (or be called by) native applications. Using JNI, one can call methods written in C/C++ or even access assembly language.

Method 3. Use JNA

Java Native Access (JNA) is an open source project that makes it possible for you to use native libraries using only Java code, with little to no C programming experience.

External references

Leave a Reply

Your email address will not be published. Required fields are marked *