Calling a C program from java

Java provides us with a native interface which allows calling native programs (code) like C, C++, etc. and this interface is called as Java Native Interface (JNI).

Java Native Interface(JNI): The Java SDK’s Java Native Interface (JNI) is a programming framework that enables Java code running in a Java Virtual Machine (JVM) to call, and to be calledby, native applications (programs specific to a hardware and operating system platform) and libraries written in other languages such as C, C++ and assembly.

At compile time, JNI defines how Java data types correspond to C data types. C programs get this

Information from JNI header files that come with the Java SDK.

The header files for specific application/program are created using a tool named Javah.

Javah: A tool that comes with the SDK, creates application-specific header files that helps to eliminate programming errors in communication between Java and C routines.

At run time, JNI lets Java objects be passed to C code, and lets C code access Java properties and methods. Thus, C code can set properties of Java classes, making it possible to call Java methods from C and vice versa.


JNI acts as an intermediate between Java Code and  other native languages, this calling of native code can be used to avoid the rewriting the code in java, and implements the functionality of code reusability.

Some simple steps to call a c method from java program:

Step-1: Create a Java program that contains the declaration of the C methods as native. 

Step-2: Compile the java program. 

Step-3: Use the Javah tool to create a header file that corresponds to the Java program.          

Step-4: Write the C code and compile the program.                                    

Step-5: Generate the shared library.                

Step-6: Execute the Java program.


Step-1: Creating a java program with declaration of C methods as native

In our example we will take a simple example with a welcometoC() method which is a native method of C to be called from java program.

The code to call the native method is:

public native void welcometoC(); 

The above declaration instructs the Java compiler that the program calls a native method. This declaration enables the integration of a C method (welcometoC) within a Java program.

After the native method is declared, you need to load the library that contains the implementation of the method you are going to call in the Java code. The following statement loads the library:

static {

So the complete java program is:-

public class Demo {

// Declaration of the native method

public native void welcometoC();

/*The native keyword tells the compiler that the implementation of this method is in a native language*/
/*Loading the library containing the implementation of the native method*/

    static {
//invoking the native method
    public static void main(String[] args)
        new Demo().welcometoC();

Step-2: Compile the java program   Compile the java program with javac compiler. During compilation, the compiler includes information about the native methods declared in the code. This helps to develop the header file.

 Step-3: Use the Javah tool to create a header file that corresponds to the Java program                   

For creating the header files use the Javah tool, which is available in the JDK in the Bin folder.

Do place the header file in the include directory in the JDK folder, as this file provides the signature of the native method declared in the java program.

The header file provides the following signature:

Java_Demo_welcometoC(JNIEnv*, jobject);

  • JNIEnv*: Represents an interface pointer.
  • jobject: Signifies a reference to the Java object for which the native method is invoked.

The JNIEnv* is the pointer to the thread that invokes a native method. This pointer is used by the C implementation code to access objects or variables from the Java code.

The header file is generated as shown below:


/* DO NOT EDIT THIS FILE - it is machine generated */

#include <jni.h>

/* Header for class Demo */

#ifndef _Included_Demo

#define _Included_Demo

#ifdef __cplusplus

extern "C"




* Class:     Demo

* Method:    welcometoC

* Signature: ()V


JNIEXPORT void JNICALL Java_Demo_welcometoC (JNIEnv *, jobject);

#ifdef __cplusplus




//end of Demo.h

Step-4: Write the C code and compile the program

// DemoC.c

#include <stdio.h>

#include <jni.h>

#include "Demo.h"

//definition of methodOfC()


JNIEXPORT void JNICALL Java_Demo_welcometoC (JNIEnv *env, jobject javaobj)


printf("Hello this is C program\n");

printf("Going back to Java program…");



In the above C program some terms to get known are:

  • jni.h: Provides the interface between Java and C.
  • Demo.h: Represents the header file generated from Demo.class.
  • stdio.h: Represents a standard library C header file needed for printf().

Now to compile the c program we need the c compiler, so I have used the Tiny C. Download and unzip the file and set the OS path to tcc.exe

Step-5: Generate the shared library

To generate the shared library file dll use the following command:

tcc DemoC.C -I C:/Progra~1/Java/jdk1.7.0_07/include -I C:/Progra~1/Java/jdk1.7.0_07/include/win32 -shared -o Demo.dll

Two directories are included in the compile command, those are to include the jni.h and jni_md.h

Step-6: Execute the Java program

As we are done with all the compilations, run the java program with specific java tool.


Hello this is C program

Going back to Java program…

Your Queries are always welcomed🙂

Speak Your Mind

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: