Integrating So,Dll for calling c program from Java

Java Program

Class JavaCallingC{

            Public static void main(String args[]){

            System.out.println(“Hi This is by Java from java”);

                        }

          }
The above program is simple and plain printing Hi This is by Java from java Now what if we want this same string to be printed by calling a C program
Before doing it let us first understand some required concepts
javah

Javah generates the header files and source files that are used by the C program to reference an objects instance variables from native source code the name of the header file is same as that of class that is say if the class name is xyz.class its corresponding header file will have a name xyz.h

javah takes the .class files to generate the header files remember to compile the java program before generating the header files

Changing JavaCallingC.java to call C program
Class JavaCallingC{
public native void callingC();    
  static  {
            System.loadLibrary(“CJava”);//CJava is shared lib name
             }
                public static void main(String args[]){
                       new JavaCallingC().callingC();
      }
            }
native keyword indicates that this method is implemented in native code using JNI
JNI (Java Native Interface)
JNI helps the java program to call a function written in C/C++ but at the cost of losing the crux of java the platform portability
JNI specifies to declare the native methods with native keyword and no body
Eg.   public native void callingC(); 

We need to make sure that the shared library is loaded before the native method is called this is the reson why we are calling the System.loadLibrary…. in static block Since all the statements in static block are executed when the class is loaded
as they are  loaded when  class is ..
JNI specifies what should be the syntax of native method being called

JNIEXPORT void JNICALL Java_NativeMethods_nativeOne(JNIEnv * env, jobject thisObj)
as our method name is calling therefore it will be define like
JNIEXPORT void JNICALL Java_JavaToC_callingC(JNIEnv *env, jobject javaobj) 
{
          printf(“Hi This is by Java from c”);
 return;
}
C Program
#include <stadio.h>
#include “JavaCallingC.h”
#include <jni.h>

JNIEXPORT void JNICALL Java_JavaToC_callingC(JNIEnv *env, jobject javaobj) 
{
printf(“Hi This is by Java from c”);
 return;
}
Save it as JavaCallingC.c

JavaCallingC.h is generated by doing

javah JavaCallingC

jni.h is present in JDK under include folder
Since our c program not has main so we can not make a executable from it but yes we can compile it generate the corresponding .obj file and then a shared library using this .obj file
We need to generate the Position Independent Code(PIC) for shared library as it helps the library to load at any address instead of same fixed address

In Linux we can generate it by doing
gcc –c –Wall -Werror -fpic –I/usr/lib/jvm/<ur java jdk path uptill include> JavaCallingC.c

Since we are using jni.h and it is present in include in our SDK folder we give path to the header file using -I
-fpic to genarte position independent code
-c to compile
-Werror to convert any warnings into error

This will  generate the .obj file JavaCallingC.o
creating a shared library

gcc –shared  -o libCJava.so JavaCallingC.o
on windows generate libCJava.dll
This creates the shared library libCJava.so
whatever u want to name u have to prefix it by lib word like libxyz.so
while we named our generated shared library as libCJava.so we will be using “CJava” as name to load it in our java program
System.loadLibrary(“CJava”);//CJava is shared lib name

Now you only need to specify the path of jni.h,jni_md.h and .so file in the build path in eclipse this can be done by right click project
Properties–>java build path–>select Lib tab–>expand JRE–>select native library–>click on edit–>specify the folder containing the files

Run the program !!!!