Calling C Code from Java

Hello All, over this week end one of my friends asked me to call a C Code from Java, As C has got its own advantages in programming domain, in fact i am a java developer but personally feel “C is Powerful than Java in some aspects”.
Now in this below tutorial i will demonstrate how to call “C code”(Native code) from java Using one of the popular API of java called “JNI”(Java Native Interface).

First of all lets start with the java code as below.

public class JavaToC {
	public native void helloC();
    static {
    public static void main(String[] args) {
        new JavaToC().helloC();

Once we have java code let us follow the below steps:

  • Lets make use of the standard tool provided in jdk called ‘javah’, used for generating the header file from java code
  • use the generated header file in the subsequent C code
  • before generating the header file, please compile the java file
  • use : javah JavaToC

the generated header file looks as below.

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

#ifndef _Included_JavaToC
#define _Included_JavaToC
#ifdef __cplusplus
extern "C" {
 * Class:     JavaToC
 * Method:    helloC
 * Signature: ()V
JNIEXPORT void JNICALL Java_JavaToC_helloC
  (JNIEnv *, jobject);

#ifdef __cplusplus

Now its time for us to code the C source file as below.

#include <stdio.h>
#include <jni.h>
#include "JavaToC.h"
JNIEXPORT void JNICALL Java_JavaToC_helloC(JNIEnv *env, jobject javaobj)
  printf("Hello World: From C");

Please note the below are few points regarding the above C Code.

  • stdio.h is the standard C header file include.
  • jni.h is the bridge header file between the java and C languages, also its available in standard jdk
  • Please note that you can see JavaToC.h file after you issue javah JavaToC command

Now, the major thing comes here, to access C Code from java we need to make the C Code as a shared Lib, inorder to do this we need to hava C Compiler already installed in our system. There are many C Compilers available in market as Turbo C, Cygwin MingW, Tiny C etc, for now i used Tiny C in this example. Here are the instruction to Download and generate the Shared Lib using Tiny C.

  • Please Download the Tiny C from here
  • Unzip the file and set OS path to tcc.exe
  • tcc HelloWorld.c -I D:\Java8\jdk1.8.0_05\include -I D:\Java8\jdk1.8.0_05\include\win32 -shared -o HelloWorld.dll
  • The above command is used for generating the shared Lib (.dll), also the above command is argumented with two folders which contain jni.h and jni_md.h file from standard jdk

Thats all folks, if you are set and every thing went smooth, now its time to call Java code using “Java JavaToC”.
finally the output is as follows.

Happy JNI 🙂


Leave a Reply

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

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

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s