How to Write Native Methods in Java

Mehvish Ashiq Feb 16, 2024
  1. What Are Native Methods in Java
  2. Write Native Methods in Java Programs
How to Write Native Methods in Java

This article takes you through various steps necessary to learn how to write native methods in Java programs.

What Are Native Methods in Java

The native keyword is a modifier in Java programming, used with methods only. It represents that this particular method is written in the native code via Java Native Interface (JNI).

The native (or foreign) methods are implemented in C, C++, or any other platform-dependent code.

Syntax:

[public/protected/private] native [returnType] youMethodName();

Write Native Methods in Java Programs

Listed below is the multi-step process to write native methods in Java programs:

  • Write the Java program containing the native method’s declaration and the main method to call the native method.
  • Compile the Java program with the main method and declare the specified native method.
  • Use javah with -jni flag to create the header file for the native method.
  • Write the code for the native method in the programming language you like, for instance, C/C++.
  • Create the shared library to compile the implementation and header files.
  • Finally, execute/run the Java Program.

Let’s follow all of these steps to call native methods in the Java program.

Write a Java Program

class nativeDemoClass {
  // declaration of the native method
  public native void showMessage();

  // load native C library
  static {
    System.loadLibrary("nativedemoclass"); // lowercase of classname
  }

  // java main method
  public static void main(String[] args) {
    nativeDemoClass demo = new nativeDemoClass();
    demo.showMessage();
  }
}

We must include the native keyword as a method’s declaration part in the Java program if we implement the method in a programming language other than Java.

This native keyword tells the Java compiler that this specific method is a native programming language method. Remember, the declaration of a native method in the Java program provides the method signature only.

We have a static block that loads the native C library in which we have written the implementation of the showMessage() method. Have you noticed that we are passing a string to the System.loadLibrary() method?

Why is it so? It is because this static block from the nativeDemoClass class will load the appropriate library, named nativedemoclass.

Let’s move to the second step and compile the Java program.

Compile the Java Program

Use the command given below to compile the Java program.

javac nativeDemoClass.java

After successfully compiling the Java program, we get the nativeDemoClass.class file.

Create a Header File

Use the following command to create a header file.

Here, -h produces the C/C++ header files and places the generated file into the specified directory. We use dot (.) to keep the generated header file in the current directory.

javac -h . nativeDemoClass.java
Note
You may find on the Internet that javah creates C/C++ header files, which was correct until JDK 8. According to the oracle, javah is deprecated now, but we have an alternative solution given above.

The nativeDemoClass.h file looks as follows. We do not edit this because it is a machine-generated file.

/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class nativeDemoClass */

#ifndef _Included_nativeDemoClass
#define _Included_nativeDemoClass
#ifdef __cplusplus
extern "C" {
#endif
/*
 * Class:     nativeDemoClass
 * Method:    showMessage
 * Signature: ()V
 */
JNIEXPORT void JNICALL Java_nativeDemoClass_showMessage(JNIEnv *, jobject);

#ifdef __cplusplus
}
#endif
#endif

The JNIEXPORT void JNICALL Java_nativeDemoClass_showMessage declares the C function in this header file. We can have multiple function signatures if the nativeDemoClass has more native methods.

The native language method’s name, which implements the native method, contains the following:

  • Prefix as Java_
  • The package name
  • The class name
  • native method’s name

Each section is separated by an underscore (_), as shown graphically below:

Write Native Methods in Java - Naming Convention

Further, the JNIEnv* refers to the Java Native Interface environment that allows us to access the Java Native Interface (JNI) methods. The jobject references this object of Java programming.

Write the Native Code

#include <jni.h>
#include <stdio.h>

#include "nativeDemoClass.h"

JNIEXPORT void JNICALL Java_nativeDemoClass_showMessage(JNIEnv *env,
                                                        jobject obj) {
  printf("Hello world!\n");
  return;
}

The implementation of Java_nativeDemoClass_showMessage is pretty straightforward because we are only using a printf() statement to print Hello world. Remember, this .c file has three headers, jni.h, stdio.h, and nativeDemoClass.h created in the previous step.

Create the Shared Library

Use the following command to create the shared library. We must have build tools for visual studio to use the CL command.

cl -Ic:\java\jdk-17.0.2\include -Ic:\java\jdk-17.0.2\include\win32 -LD nativeDemoClassImp.c -Fenativedemoclass.dll

Remember, the Java program uses the following method to load the shared library, named nativedemoclass at run time.

System.loadLibrary("nativedemoclass");

Run the Java Program

Finally, use the command given below to run the Java program.

java nativeDemoClass

You will see Hello world! on the command line if you have done everything correctly.

Output:

Write Native Methods in Java - Output

Mehvish Ashiq avatar Mehvish Ashiq avatar

Mehvish Ashiq is a former Java Programmer and a Data Science enthusiast who leverages her expertise to help others to learn and grow by creating interesting, useful, and reader-friendly content in Computer Programming, Data Science, and Technology.

LinkedIn GitHub Facebook

Related Article - Java Method