Tuesday 6 August 2013

Java Native Interface


A programming interface where java programming language interacts with native programming languages such as C/C++. As a programmer I found it hard to resolve away to communicate with native languages especially C which java derives much of its syntax. The following sample code worked well with C and C++ on windows platform and may also work well on other platforms like UNIX.
I'll give first C sample code which you must have the compilers configured before doing any of these procedures.

Using C language;

First open your editor and save the code below in a java source file (.java extension), that is, "HelloWorld.java".

class HelloWorld {
private native void print();
public static void main(String[] args) {
new HelloWorld().print();
}
static {
System.loadLibrary("HelloWorld"); // loads HelloWorld.dll library file
}
}


then save the code below in a C source file (.c extension), that is, "HelloWorld.c".

#include <jni.h>
#include <stdio.h>
#include "HelloWorld.h"
JNIEXPORT void JNICALL
Java_HelloWorld_print(JNIEnv *env, jobject obj)
{
printf("Welcome to JNI using C!\n");
return;
}


Lastly you should have a batch script to execute your commands so save the following contents in "build.bat" file.

javac HelloWorld.java
javah HelloWorld
cl /I"C:\Program Files\Java\jdk1.7.0_01\include" /I"C:\Program Files\Java\jdk1.7.0_01\include\win32" /I"C:\Program Files\Microsoft Visual Studio\VC98\Include" /I"C:\Program Files\Microsoft Visual Studio\VC98\Lib" HelloWorld.c /Fe"HelloWorld.dll" /LD
java HelloWorld


You should have a similar output.



Using C++ language;
Open your text editor and save the following contents in a .java file, that is, "JNIPrintWrapper.java".


// File: JNIPrintWrapper.java
// Allows access to native methods

public class JNIPrintWrapper
{
    // load library JNIPrintLibrary into JVM
    static
    {
        System.loadLibrary( "JNIPrintWrapper" );
    }
    // native C++ method
    public native void printMessage( String message );
 }


save the following in a java source file, that is, "JNIPrintMain.java".

// File: JNIPrintMain.java
// Creates a new instance of the Java wrapper class,
// and calls the native methods.

public class JNIPrintMain{

// uses JNI to print a message
 public static void main( String args[] )
{
 JNIPrintWrapper wrapper = new JNIPrintWrapper();

 // call to native methods through JNIWrapper
 wrapper.printMessage( "Welcome to JNI using C++!\n" );
 }
}


save the following in a C++ source file, that is, "JNIPrintWrapperImpl.cpp".

// File: JNIPrintWrapperImpl.cpp
 // Implements the header created by Java
 // to integrate with JNI.

// C++ core header
 #include <iostream.h>

// header produced by javah
 #include "JNIPrintWrapper.h"

 // prints string provided by Java application
 JNIEXPORT void JNICALL Java_JNIPrintWrapper_printMessage
 ( JNIEnv * env, jobject thisObject, jstring message )
 {
 // boolean to determine if string is copied
 jboolean copied;

 // call JNI method to convert jstring to cstring
 const char* charMessage =
 env->GetStringUTFChars( message, &copied );

 // print message
 cout << charMessage;

// release string to prevent memory leak
env->ReleaseStringUTFChars( message, charMessage );
}



You should have a similar output as shown below.



No comments:

Post a Comment