Applets calling native methods

More Articles

J2ME and the 6600.

Megaupload - File upload progress bar.

Signed Applet Howto

Can applets call native methods? The answer is yes. Since there is alwasy more than one way to fold a map please be 100% certain that you cannot do the same thing with with the java libraries. If you are certain that the functionality you are looking for can only be found in native code, lets get started. You will need to know how to create signed applets. If not, please take a moment to read the article on this topic

Before we actually build the applet we will build a standalone client. It will allow us familiarize ourselves with the Java Native Interface. If you are already using JNI in your work, you can skip ahead to the next step.

The listing below is of the obligatory hello world program. It is different from what you have seen in your java primer though; Instead of calling System.out.println(), it will call a C function. It is this C function that will in print out the message.


	package com.raditha.articles.jni;
	
	public class NativeHelloWorld
	{
		public native void displayHelloWorld();
	
	
		public static void main(String[] args) {
			System.load(System.getProperty("user.home") +"/libhello.so");
			new NativeHelloWorld().displayHelloWorld();
		}
	}

Notice the type modifier native? It tells the compiler that we are calling a function in an external library. We use the load() method to load the library containing our native code. The official java tutorial uses the loadLibrary() call. The latter has a few pitfalls. The first is that libraries on different platforms have different naming conventions. These are not always obvious to java programmers. The second issue is that loadLibrary() will only look in predefined folders for the required library. That rules it it's usage with applets.

Once we have compiled the java code, we need to create the header file for use with our C code. You IDE probably will not include this functionalty. The header file will have to be generated manullay. To do so; Enter the directory where your compiled classes are located and type in:

   javah com.raditha.articles.jni.NativeHelloWorld
Click here, If you want to see what the header file looks like. The listing for the C code that prints the string Hello World, is given below. (all the relevent files can be found in jni.zip).

	#include 
	#include "com_raditha_articles_jni_NativeHelloWorld.h"
	#include 
	
	JNIEXPORT void JNICALL 
	Java_com_raditha_articles_jni_NativeHelloWorld_displayHelloWorld(JNIEnv *env, jobject obj) 
	{
		printf("Hello world!\n");
	}

This is how you compile the code into a library:


    gcc -o libhello.so  -I$JAVA_HOME:include/linux/ HelloWorldImp.c -shared

If you are not using linux (that's too bad) you will have to read your compiler's documentation about how to create a library. On windows you will create a DLL instead of a SO. Move libhello.so to your home folder and type in the line below to execute the program we have just built. When you are done we will move on to the next step and see how our java code can accept a return value from native code.


    java com.raditha.articles.jni.NativeHelloWorld
Goto Page : 1  ,  2 ,  3
Copyright © Raditha Dissanayake 2013