An Overview of the JNI
Using the JNI, Java can interact with any existing applications or libraries written in C or C++, without the need to rewrite the existing code. In addition, you can use the JNI and libraries win C or C++ when the standard Java class library does not provide platform-specific features.
JNI Architecture
When a Java program is compiled, Java produces platform-independent byte code. Conversely, when a C program is compiled, it produces host-specific code that can only be run on the type of platform on which it was compiled. Any program compiled in a native language depends on the native libraries used, the operating system, and the hardware, such as the CPU instruction set.
Attachment 619
Advantages and Disadvantages of Calling a C Program from Java
When a C program is called from Java, it provides the following advantages:
- Eliminates the need to rewrite the functions performed by a C program in Java.
- Enables Java applications to use the standard C library.
- Enables you to include a host-specific feature in a Java application by writing a C program, which then interacts with the Java application.
- Enables you to write in C a part of an application that has to be executed.
The disadvantages of calling a C program from Java are:
- Develops an outcome application that is not platform-independent.
- Renders the outcome application insecure.
- Cannot integrate applets.
- Makes it difficult to debug the resultant code.
Tools Used
To integrate a C program in a Java program, you can use the following tools available with the JDK:
- javah: Generates a header file that corresponds to a Java program.
- javap: Generates signatures of the native methods declared in a Java program.
javah
To call a C program from Java, you need to create a header file that corresponds to the Java program. To achieve this, you can use the javah tool. The header file name is the same as that of the Java class with a .h extension added to it.The syntax of javah is:
javah [options] <name of the Java class> You use the -jni option to generate a JNI style header file.
javap
The javap is a class file disassembler tool available with the JDK. You can use this tool to generate the method signatures, eliminating the chance of errors that may occur if manual signatures are generated.The syntax of javap is:
javap [option] <name of the Java class> For JNI, use the following options with javap:
- -s: Generates a signature.
- -p: Includes the signatures of private members.
Next:- Steps To Call A C Program From Java?


Currently Active Users
Categories
Latest Blog Entries

Rate this article