Calling a C program may be useful when we prefer to use C libraries and to reuse an existing C program. When we compile a C program, the source gets converted to obj file. It is a platform dependent intermediate machine code which will be converted to exe and then executed.
Java native interface (JNI) is a framework provided by java that enables java programs to call native code and vice-versa.

Using JNI a java program has the capability to call the native C code. But we lose the core objective of java which is platform independence. So calling a C program from java should be used judiciously.
JNI provides the specification and native code should be written/ported accordingly. JDK vendor should provide the needed implementation for the JNI.
Steps to call a C program from Java
1. Write the Java Program and Compile
2. Generate header file from java class
3. Write the C Program
4. Generate Shared Library File
5. Run Java Program
1. Write the Java Program
public class JavaToC {
public native void helloC();
static {
System.loadLibrary("HelloWorld");
}
public static void main(String[] args) {
new JavaToC().helloC();
}
}
Note two things in this program,
- Use of native keyword. This is a method declaration and it informs the java compiler that the implementation for this method is a native one. This method helloC() is present in C source file and that is what we are going to call.
- Loading the library HelloWorld using static keyword. This library file will be compiled out of the C source in the coming steps.
2. Generate header file from java class
- JDK provides a tool named javah which can be used to generate the header file.
- We will use the generated header file as include in C source.
- Remember to compile the java program before using javah
javah JavaToC
Following is the header file generated,
/* DO NOT EDIT THIS FILE - it is machine generated */
#include
/* Header for class JavaToC */
#ifndef _Included_JavaToC
#define _Included_JavaToC
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: JavaToC
* Method: helloC
* Signature: ()V
*/
JNIEXPORT void JNICALL Java_JavaToC_helloC
(JNIEnv *, jobject);
#ifdef __cplusplus
}
#endif
#endif
3. Write the C Program
#include <stdio.h>
#include <jni.h>
#include "JavaToC.h"
JNIEXPORT void JNICALL Java_JavaToC_helloC(JNIEnv *env, jobject javaobj)
{
printf("Hello World: From C");
return;
}
- stdio.h is the standard C header file include.
- jni.h is the header file that provides the java to C mapping and it is available in JDK.
- JavaToC.h is the header file generated from the java source file.
4. Generate Shared Library File
- Now compile the above C source file. We need a C compiler and I have chosen Tiny C.
- Tiny C can be downloaded from http://mirror.veriportal.com/savannah//tinycc/tcc-0.9.25-win32-bin.zip
Download and unzip the file and set OS path to tcc.exe - tcc HelloWorld.C -I C:/Progra~1/Java/jdk1.7.0_07/include -I C:/Progra~1/Java/jdk1.7.0_07/include/win32 -shared -o HelloWorld.dll
- Above is the command to generate the shared library file dll which is loaded in the java program. Two directories are included in the compile command, those are to include the jni.h and jni_md.h
5. Run Java Program
We are all set, now run the java program to get the following output,
Hello World: From C













typo: In section 3. your #includes seems to be missing.
applay this….add quotes..
tcc HelloWorld.C -I “C:/Progra~1/Java/jdk1.7.0_07/include” -I “C:/Progra~1/Java/jdk1.7.0_07/include/win32″ -shared -o HelloWorld.dll
Hey thanks, fixed the typo.
i was anxiosly wating for this topic to post
Cool stuff!
How does the process differ for Android NDK?
Thanx … Because of you i am able to understand the concept of Java native interface (JNI) .
Thanks a lot.. Very useful and nice one..
Nice Article…
Please explain about below line…
JNIEXPORT void JNICALL Java_JavaToC_helloC
(JNIEnv *, jobject);
nice article joe…:)
Really Helped…Thanks a lot… :)
hello joe thanx for the article
plz write some article about oops concept with example …….from the point of interview…
Nice writeup – JNI is great for libraries but the overhead of the native call if a bit of a pain. If you only need to call a program (i.e. not a library) you should check out NestedVM http://nestedvm.ibex.org/. It allows you to compile C to Java byte code and run it on the JVM.
Hello Sir !!! Thanks For Helping Us Regularly…
Good Article Joe.. keep it up
Hi joe,
Facing issue while compilation
tcc HelloWorld.C -I C:/Progra~1/Java/jdk1.7.0_07/include -I C:/Progra~1/Java/jdk1.7.0_07/include/win32 -shared -o HelloWorld.dll
Following error occured during compilation of Helloworld.c:
D:\Data\Personal\java\c4mjava>tcc HelloWorld.c -I C:\Program Files\Java\jdk1.7.0_03\include -shared -o HelloWorld.dll
HelloWorld.c:2: include file ‘jni.h’ not found
Please give solution to this issue..
Thanks
apply this….add quotes..
tcc HelloWorld.C -I “C:/Progra~1/Java/jdk1.7.0_07/include” -I “C:/Progra~1/Java/jdk1.7.0_07/include/win32″ -shared -o HelloWorld.dll
nice !!!
its good informative article. thanks for share with us.
How to generate shared library file for the linux platform ?
Its nice … Got an idea about JNI … Thnx …Keep writing :)
I was waiting for this. Nice example sir. Thanks alot
Hi, i am getting the following error. Could you help me resolving this error?
C:\java\javatoc>tcc HelloWorld.C -I C:/Progra~1/Java/jdk1.7.0_07/include -I C:/Progra~1/Java/jdk1.7.0_07/include/win32 -shared -o HelloWorld.dll
HelloWorld.C:2: include file ‘jni.h’ not found
Hello Sir,
I tried this before I did all the steps correctly but I could not be create the necessary .dll file. Should I have to install Ms visual studio for creation of .dll file. because without that i am not able to use cl command to create .dll file. Or else Please tell me the another way of creation. So that I can complete this assignment for that Please help me out Sir.
Thankyou
Nice Article!!!!!!!!!!!!!!!!!!!!!
NICE AND SIMPLE program……
vry nice articleee………
As always great articles, never know it was that easy.
nice article ……………
very helpful for java developers………
Nice….
very useful for java developers.
Thanks.
Nice one. We have used this previously.
This one is very useful in password encryption logic.
very clear understand….and easy to catch it.thanks
Hello Sir,
Its a nice tutorial. Thank you so much :)
I have one problem
I am trying this with gcc on windows platform but I got this error:-
Exception in thread “main” java.lang.UnsatisfiedLinkError: JavaToC.helloC()V
at JavaToC.helloC(Native Method)
at JavaToC.main(JavaToC.java:9)
following the http://stackoverflow.com/questions/11420153/java-lang-unsatisfiedlinkerror-returned-when-calling-native-method answer I put underscore in header file and in my c file method name _Java_javaToC_helloC and It worked fine. Can you please explain this dependency.
Nice post…..
Good article Joe..
Adding some more explanations will help us to understand better…
I am very sorry to say joe, from last two weeks javapapers site design not al;ignd properly.all articles tab missing and footer of site not good.
I don’t have your contact information that’s wise i am choosing this place.
Pyla, I am working of footer, comment and menu redesign. Will fix them soon. Thanks.
good to see this article. thanks a lot.
Enclose the path with quotes:
tcc HelloWorld.C -I “C:\Program Files\java\jdk1.6.0_13\include” -I “C:\Program Files\java\jdk1.6.0_13\include/win32″ -shared -o HelloWorld.dll
really superb,a great explanaition
Nice post…..
Thanks joy grate work!
try this
This is a very interesting blog for java readers.
Thanks joe.
Good stuff Joe. Helped me a lot to understand JNI.
Hello,
When I creating the .dll file at that time, it is saying tcc is not recongnised as internal or external command
I downloaded tcc.zip. Inside it tcc.exe file is there. Then set path in the system evvironment variable. copied the path of tcc.exe with filename(tcc.exe) file and pasted in the path variable. Then i tried to run the command tcc ….
it gives tcc is not recognised as internal or external command. Please help!
Good explanation of the concept
Thanks Praveen.
it a innovative thinking ,so good, very helpfull
Hi Joe,
Great… I found this blog very interesting….
If I need to keep in touch with you, what will be your email address… (if you have no reservations to share your mail id)…
Thanks and regards
Balaji / balajihx@gmail.com
Guys who are facing “HelloWorld.C:2: include file ‘jni.h’ not found” this problem please use following command. I was getting same issue but it was solved when I used this Command:
C:\Java2C\tcc>(path to tcc folder)
tcc HelloWorld.c -I “C:\Program Files\Java\(whatever version u r using)jdk1.7.0_07\include” -I “C:\Program Files\Java\jdk1.7.0_07\include/win32″ -shared -o HelloWorld.dll
Hi Joe,
Is there any solution for this:
Because I really wish to see the program running…
C:\Java2C\tcc>java JavaToC
Exception in thread “main” java.lang.UnsatisfiedLinkError: C:\Java2C\tcc\HelloWo
rld.dll: Can’t load IA 32-bit .dll on a AMD 64-bit platform
at java.lang.ClassLoader$NativeLibrary.load(Native Method)
at java.lang.ClassLoader.loadLibrary1(Unknown Source)
at java.lang.ClassLoader.loadLibrary0(Unknown Source)
at java.lang.ClassLoader.loadLibrary(Unknown Source)
at java.lang.Runtime.loadLibrary0(Unknown Source)
at java.lang.System.loadLibrary(Unknown Source)
at JavaToC.(JavaToC.java:6)
u are trying to load 32 bil .so files in 64 bit OS.
first,compile with 64 bit then try to load in 64 bit environment
I have the same issues
C:\Dev\JNITest>c:\Dev\tcc\tcc.exe HelloWorld.C -I C:\Java\jdk1.7.0_05\include -I C:\Java\jdk1.7.0_05\include\win32 -shared -o HelloWorld.dll
C:\Dev\JNITest>dir
Volume in drive C is Windows
Volume Serial Number is 2EC0-F1BC
Directory of C:\Dev\JNITest
02/14/2013 05:26 PM .
02/14/2013 05:26 PM ..
02/14/2013 05:17 PM 184 HelloWorld.c
02/14/2013 05:26 PM 59 HelloWorld.def
02/14/2013 05:26 PM 2,048 HelloWorld.dll
02/14/2013 05:04 PM 450 JavaToC.class
02/14/2013 05:06 PM 385 JavaToC.h
02/14/2013 05:04 PM 218 JavaToC.java
02/14/2013 05:02 PM JNIExample
6 File(s) 3,344 bytes
3 Dir(s) 296,163,921,920 bytes free
C:\Dev\JNITest>java JavaToC
Exception in thread “main” java.lang.UnsatisfiedLinkError: C:\Dev\JNITest\HelloWorld.dll: Can’t load IA 32-bit .dll on a AMD 64-bit platform
at java.lang.ClassLoader$NativeLibrary.load(Native Method)
at java.lang.ClassLoader.loadLibrary1(ClassLoader.java:1939)
at java.lang.ClassLoader.loadLibrary0(ClassLoader.java:1864)
at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1854)
at java.lang.Runtime.loadLibrary0(Runtime.java:845)
at java.lang.System.loadLibrary(System.java:1084)
at JavaToC.(JavaToC.java:6)
GOT IT, download the 64bit Tiny C. build the DLL
http://mirror.veriportal.com/savannah//tinycc/
Help for command for create dll file
really excellent tutorial.i have been looking for it for a log time.THANK YOU VERY MUCH
Hi i have to load .so file in cpp program and will have to call the method of same .so files.
plz any help me out.
when I compile this file this erroe show
include file ‘jni.h’ not found