• Loading
    • Steps to Call a C Program from Java

      Previous Article:- How To Call C Program From Java?

      The steps used to call a C program from Java helps convert and generate forms that enable communication between C and a Java program. To call a C program from Java:
      • Create a Java file that contains the declaration of the C methods as native.
      • Compile the Java file.
      • Use the javah tool to create a header file that corresponds to the Java file.
      • Write the C code.
      • Compile the C code and load the shared library.
      • Execute the Java file.


      Creating the Java Program

      To call a C program from Java, you first need to write a Java program with the declarations of the methods that are to be called from the C program. These declarations must be preceded by the native keyword. For example, you can make the following declaration in a Java program to call the display() method from a C program:

      Code:
      public native void display();
      The above declaration instructs the Java compiler that the program calls a native method. This declaration enables the integration of a C method 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:

      Code:
      System.loadLibrary("<name of the library>");
      You should always use the above statement within a static block. For example, to load a library named try, include this code:


      Code:
      static { 	System.loadLibrary("try"); }
      Note: Although the library that contains the implementation of the native code has a .dll extension, the extension is not included in the statement.



      Code:
      //Demo.java
      class Demo 
      {
          // Declaration of the native method
          public native void methodOfC();
      	/*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 
          {
              System.out.println("Control with Java.......going to call a C program");
              System.loadLibrary("try");
          }
          public static void main(String[] args) 
          {
              //invoking the native method  
              new Demo().methodOfC();
              System.out.println("Receiving control back from C");
          }//end main
      }//end Demo
      In the above code, the native method is declared with a native keyword preceding it. Next, the library is loaded and a call is made to the native method, its implementation is provided by the loaded library.

      Compiling the Java Code
      The Java code is compiled with the javac compiler. During compilation, the compiler includes information about the native methods declared in the code. This helps to develop the header file.

      Creating the Header File
      To create the header file, use the javah tool, which is available with the JDK and is present in the bin directory.

      Place the header file in the include directory within the JDK folder. The header file provides the signature of the native method declared in the Java code.

      Note:The native method signature in the header file and the signature in the implementation code must be the same.

      The native method signature generated by the header file contains a Java prefix, a class name, and a method name. For example, the following declaration is present in the Demo.java Java file:

      public native void display()

      The header file provides the following signature:

      Java_Demo_display(JNIEnv*, jobject);

      In addition to any parameters included in the method declaration, the native method signature contains two parameters by default. These parameters are:

      • 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.

      Code:
      //Demo.h
      /* 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" 
      {
      	#endif
      	/*
      	 * Class:     Demo
      	 * Method:    methodOfC
      	 * Signature: ()V
      	 */
      	JNIEXPORT void JNICALL Java_Demo_methodOfC (JNIEnv *, jobject);
      	#ifdef __cplusplus
      }
      #endif
      #endif
      //end of Demo.h
      This code shows the header file generated by javah.

      Caution :Do not modify this code manually because it may lead to the inability to call a native method inside a Java code.

      The header file should be regenerated each time the Java file is modified.

      Implementing the C Method
      After the header file is generated, you need to implement the native method.

      Code:
      //DemoImp.c
      #include <jni.h>
      #include "Demo.h"
      #include <stdio.h>
      //definition of methodOfC()
      JNIEXPORT void JNICALL 
      Java_Demo_methodOfC(JNIEnv *exeenv, jobject javaobj) 
      {
      	printf("This is in the C program\n");
      	printf(".......Back to Java\n");
      	return;
      }

      This listing shows how the methodOfC() native method is implemented. The following files are included in the implementation:

      • 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().



      The implementation of the method starts with the signature similar to that generated by the Demo.h header file. You can also generate these signatures by using the javap tool.

      Compiling the Native Method and Generating the Shared Library

      The native code is implemented in Microsoft Visual C. To compile the code and build the shared library, you use the cl.exe file. This file is available at:

      C:\Program Files\Microsoft Visual StudioXX\VCXX\bin

      Use the following command to compile the code and create the library:

      Cl -I<path for jni.h> -I<path for jni_md.h> -LD <path for the C file> -Fe<name of the shared library with a dll extension>


      Note : The name of the shared library should be the same as that specified in the Java code when the library is loaded.

      For example, to compile the DemoImp.c file, specify the following command at the command prompt:

      cl -Ic:\jsdk2.0\include -Ic:jsdk2.0\include\win32 -LD c:\jsdk2.0\bin\DemoImp.c -Fetry.dll

      When the cl.exe file is executed, the output is a .dll file that contains a generated DemoImp.c file.

      After the shared file is created, you can execute the Java code using the java tool.

      Next Article:- How To Use Java Primitive Datatypes In C?
    • Currently Active UsersCurrently Active Users

      There are currently 78 users online. 2 members and 76 guests

      Most users ever online was 323, 11-23-2011 at 07:47 AM.

      1. snehil2529,
      2. vbairmaclsho