How to call a C program from Java?

Last modified on August 1st, 2014 by Joe.

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,

2. Generate header file from java class

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 
#include 
#include "JavaToC.h"
JNIEXPORT void JNICALL Java_JavaToC_helloC(JNIEnv *env, jobject javaobj) 
{
	printf("Hello World: From C");
	return;
}

4. Generate Shared Library File

5. Run Java Program

We are all set, now run the java program to get the following output,

Hello World: From C

Comments on "How to call a C program from Java?"

  1. Anonymous says:

    typo: In section 3. your #includes seems to be missing.

  2. Joe says:

    Hey thanks, fixed the typo.

  3. Zahid Indher says:

    i was anxiosly wating for this topic to post

  4. Teknia says:

    Cool stuff!

    How does the process differ for Android NDK?

  5. Anonymous says:

    Thanx … Because of you i am able to understand the concept of Java native interface (JNI) .

  6. Rashii.. Coorg.. says:

    Thanks a lot.. Very useful and nice one..

  7. Pavan says:

    Nice Article…

    Please explain about below line…

    JNIEXPORT void JNICALL Java_JavaToC_helloC
    (JNIEnv *, jobject);

  8. Ankur Puri says:

    nice article joe…:)

  9. Pratheusha says:

    Really Helped…Thanks a lot… :)

  10. Anuj says:

    hello joe thanx for the article
    plz write some article about oops concept with example …….from the point of interview…

  11. John May says:

    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. It allows you to compile C to Java byte code and run it on the JVM.

  12. Raj Saini says:

    Hello Sir !!! Thanks For Helping Us Regularly…

  13. GOOTAM says:

    Good Article Joe.. keep it up

  14. Leo says:

    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

  15. Rohit says:

    nice !!!

  16. Jitendra says:

    its good informative article. thanks for share with us.

  17. saarangan says:

    How to generate shared library file for the linux platform ?

  18. Mahesh says:

    Its nice … Got an idea about JNI … Thnx …Keep writing :)

  19. Sushil Kumar Nayak says:

    I was waiting for this. Nice example sir. Thanks alot

  20. Radhakrishna says:

    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

  21. Rohit Mishra says:

    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

  22. kamal sharma says:

    Nice Article!!!!!!!!!!!!!!!!!!!!!

  23. Shaan says:

    NICE AND SIMPLE program……

  24. ravikumar says:

    vry nice articleee………

  25. Java Rohi says:

    As always great articles, never know it was that easy.

  26. sidram says:

    nice article ……………
    very helpful for java developers………

  27. Vijay Gadekar says:

    Nice….

    very useful for java developers.

    Thanks.

  28. Ashish says:

    Nice one. We have used this previously.
    This one is very useful in password encryption logic.

  29. jhansi says:

    very clear understand….and easy to catch it.thanks

  30. Preeti says:

    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)

    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.

  31. Raks says:

    Nice post…..

  32. Balaji says:

    Good article Joe..
    Adding some more explanations will help us to understand better…

  33. Pyla says:

    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.

  34. Kausik says:

    good to see this article. thanks a lot.

  35. Manohar says:

    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

  36. kamesh says:

    really superb,a great explanaition

  37. umesh says:

    Nice post…..

  38. SHIV GOVIND PATEL says:

    Thanks joy grate work!

  39. Joe says:

    Pyla, I am working of footer, comment and menu redesign. Will fix them soon. Thanks.

  40. Joe Vim says:

    This is a very interesting blog for java readers.
    Thanks joe.

  41. Avinash Babu Donthu says:

    Good stuff Joe. Helped me a lot to understand JNI.

  42. Sanjay says:

    Hello,

    When I creating the .dll file at that time, it is saying tcc is not recongnised as internal or external command

  43. Sanjay says:

    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!

  44. shyam says:

    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

  45. shyam says:

    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

  46. Praveen says:

    Good explanation of the concept

  47. anitha says:

    it a innovative thinking ,so good, very helpfull

  48. Balaji says:

    Hi Joe,

    Great… I found this blog very interesting….

  49. Tuhin says:

    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

  50. Tuhin says:

    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)

  51. Ram says:

    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)

  52. Nisha says:

    Help for command for create dll file

  53. Anonymous says:

    really excellent tutorial.i have been looking for it for a log time.THANK YOU VERY MUCH

  54. Anonymous says:

    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.

  55. Anonymous says:

    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

  56. nizam says:

    when I compile this file this erroe show
    include file ‘jni.h’ not found

  57. Akashdeep Das says:

    How to use C header files in java?

  58. Tushika says:

    nice blog…
    very knowledgeable example…
    thnxu :)

  59. Sree says:

    Explained it well. this was informative. thanks for sharing it.

  60. Anand kumar says:

    what is “JNIEXPORT void JNICALL Java_JavaToC_helloC(JNIEnv *env, jobject javaobj)
    “??? what is the use of it??

  61. Chamila Wijayarathna says:

    Hello There,
    I’m using Solaris 11, and I am failing the process when compiling C code.

    -bash-4.1$ gcc -o libHelloWorld.so -shared -I/usr/java/include -I/usr/java/include/solaris HelloWorld.c -lc
    Text relocation remains referenced
    against symbol offset in file
    .rodata (section) 0x9 /var/tmp//cc.GaGrd.o
    printf 0xe /var/tmp//cc.GaGrd.o
    ld: fatal: relocations remain against allocatable but non-writable sections
    collect2: ld returned 1 exit status

    I think I have use the right code to compiling. What is wrong here???

  62. […] a pointer to the current statement that is being executed in its thread. If the current executing method is ‘native’, then the value of program counter register will be […]

  63. Hemant says:

    Hi I am getting error, while compiling c file
    Hello.c: In function `main’:
    Hello.c:7: error: parse error before ‘{‘ token
    Hello.c:7: error: declaration for parameter “Java_JavaToC_helloC” but no such parameter

    Plz resolve it

  64. Hemant says:

    Set Path for jni.h and jni_md.h as

    C:\Program Files\Java\jdk1.6.0_26\include
    C:\Program Files\Java\jdk1.6.0_26\include\win32 to avoid giving parameters for -I

  65. Sachin says:

    Hi Joe,
    Nice Post.Actually I am calling Java Methods from C but getting Segmentation fault while creating JVM from C can you please help me in troubleshooting these probble

  66. vaibhav says:

    Hi guys,

    i am using eclipse tools with cygwin C/C++ compiler.i want to make simple demo application and create .h file and .c file successfully but not generated .dll file or .so file please help me and any solutions it’s urgent

    Error:
    Building target: HelloWorld.dll
    Invoking: Cygwin C Linker
    gcc -Xlinker -mno-cygwin -Wl,–add-stdcall-alias -shared -o “HelloWorld.dll” ./com_lithiumhead_jni_HelloWorld.o
    /usr/lib/gcc/x86_64-pc-cygwin/4.8.3/../../../../x86_64-pc-cygwin/bin/ld: unrecognised emulation mode: no-cygwin
    Supported emulations: i386pep i386pe
    collect2: error: ld returned 1 exit status
    make: *** [HelloWorld.dll] Error 1
    makefile:29: recipe for target ‘HelloWorld.dll’ failed

Comments are closed for "How to call a C program from Java?".