Accessing Native methods from a Java Applet

www.ebsolutech.com

 

by Sanket Bakshi, s.bakshi@ebsolutech.com

 

 

Previous section: Normal Applet Capabilities

The JNI Technology

1.      Introduction

2.      Design Overview

3.      The Process

Introduction

While you can write applications entirely in Java, there are situations where Java alone does not meet the needs of your application. Programmers use the JNI (Java Native Interface) to write Java native methods to handle those situations when an application cannot be written entirely in Java.

The following examples illustrate when you need to use Java native methods:

1.      The standard Java class library does not support the platform-dependent features needed by the application.

2.      You already have a library written in another language, and wish to make it accessible to Java code through the JNI.

3.      You want to implement a small portion of time-critical code in a lower-level language such as assembly.

The JNI is a standard programming interface that allows a Java programmer to write Java Native methods and also to embed the Java Virtual Machine with the native methods. The primary goal is binary compatibility of native method libraries across all Java virtual machine implementations on a given platform.

The JNI is an extended version of the NMI (Native Method Interface) that was introduced with the earlier versions of the java Development Kit (JDK) namely JDK1.0.

Design Overview

This section provides you with some basics of the JNI design.

The following topics are covered –

o       JNI Interface functions and Pointers

o       Loading and Linking Native methods

o       Native method arguments

o       Exception Handling

 

JNI Interface Functions and Pointers

Native code accesses Java VM features by calling JNI functions. JNI functions are available through an interface pointer. An interface pointer is a pointer to a pointer. This pointer points to an array of pointers, each of which points to an interface function. Every interface function is at a predefined offset inside the array.

The JNI interface pointer is only valid in the current thread. A native method, therefore, must not pass the interface pointer from one thread to another.

 

Loading Native Methods

Native methods are loaded with the System.loadLibrary method. In the following example, the class initialization method loads a platform-specific native library in which the native method f is defined:

    package pkg;

    class Cls {

         native double f(int i, String s);

         static {

             System.loadLibrary("pkg_Cls");

         }

    }

The argument to System.loadLibrary is a library name chosen arbitrarily by the programmer.

Native methods arguments

The JNI interface pointer is the first argument to native methods. The JNI interface pointer is of type JNIEnv. The second argument differs depending on whether the native method is static or non-static. The second argument to a non-static native method is a reference to the object. The second argument to a static native method is a reference to its Java class.

The remaining arguments correspond to regular Java method arguments. The native method call passes its result back to the calling routine via the return value. To pass the values or return them to or from the native functions, the JNI makes use of the JNI data types and Data structures.

The table below will describe the JNI data types and its machine dependant native equivalents –

Java Type

Native Type

Description

boolean

jboolean

unsigned 8 bits

byte

jbyte

8 bits

char

jchar

unsigned 16 bits

short

jshort

16 bits

int

jint

32 bits

long

Jlong

64 bits

float

jfloat

32 bits

double

jdouble

64 bits

 

Exception Handling

The JNI allows native methods to raise arbitrary Java exceptions. The native code may also handle outstanding Java exceptions. The Java exceptions that are not handled are propagated back to the VM.

There are two ways to handle an exception in native code:

1.      The native method can choose to return immediately, causing the exception to be thrown in the Java code that initiated the native method call.

2.      The native code can clear the exception by calling ExceptionClear(), and then execute its own exception-handling code.

After an exception has been raised, the native code must first clear the exception before making other JNI calls. When there is a pending exception, the only JNI functions that are safe to call are ExceptionOccurred(), ExceptionDescribe(), and ExceptionClear(). The ExceptionDescribe() function prints a debugging message about the pending exception.

The Process

To make JNI calls, the following process needs to be implemented –

o       Create a Stub java file that will include the signatures of all the methods that are to be accessed form the applet.

public class Native_classname

{

 public native return_data_type  Func1(param1, param2 ………);

}

            The methods have to be public and native.

 

o       Use the javah utility provided by the JDK to create a header file from the given stub file. This generates a C++ style header file.

o       Include this header file in the C/C++ code and create a library.

o       Use the loadLibrary() method in the java code to load the created library.

 

Next section: Issues in using JNI with Applets
 
Table of Contents

 


Top of Page | Home


© 2001 – EBSolute Technologies