TWiki . Dev . HandlingJNI

First some simple examples how JNI in JAVA works:

class Test 
  static {
  public static void main(String[] args) 
  private static native String runTest();

Ok, first compile the JAVA class:

javac --> Test.class

Now generate Header:

javah Test --> Test.h

This results in the following Test.h:

/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class Test */

#ifndef _Included_Test
#define _Included_Test
#ifdef __cplusplus
extern "C" {
 * Class:     Test
 * Method:    runTest
 * Signature: ()Ljava/lang/String;
JNIEXPORT jstring JNICALL Java_Test_runTest
  (JNIEnv *, jclass);

#ifdef __cplusplus

Ok, lets write a implementation Test.c:

#include <stdio.h>
#include "Test.h"

JNIEXPORT jstring JNICALL Java_Test_runTest (JNIEnv *env, jclass c) 
  jstring value;
  value = (*env)->NewStringUTF(env, "TEST-jni");
  return value;

Ok, this is a real basic implementation. But let us compile the library:

gcc -I/site.opt/Java/j2sdk1.4.2/include -I/site.opt/Java/j2sdk1.4.2/include/linux -shared Test.c -o

Finally let the example run:

java Test

Finally some links:

-- ChristianS - 20 Apr 2004

----- Revision r1.1 - 20 Apr 2004 - 04:38 GMT - ChristianS
Copyright © 1999-2003 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback.