Nice TWiki > Dev > HandlingJNI ( vs. r1.1) TWiki webs:
Dev | Doc | Main | TWiki | Sandbox
Dev . { Changes | Index | Search | Go }
 <<O>>  Difference Topic HandlingJNI (r1.1 - 20 Apr 2004 - ChristianS)
Added:
>
>

%META:TOPICINFO{author="ChristianS" date="1082435882" format="1.0" version="1.1"}% %META:TOPICPARENT{name="CurrentDiscussions"}% First some simple examples how JNI in JAVA works:

Test.java:

class Test 
{
  static {
    System.loadLibrary("test");
  }
  
  public static void main(String[] args) 
  {
    System.out.println(runTest());
  }
  
  private static native String runTest();
}

Ok, first compile the JAVA class:

javac Test.java --> 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" {
#endif
/*
 * Class:     Test
 * Method:    runTest
 * Signature: ()Ljava/lang/String;
 */
JNIEXPORT jstring JNICALL Java_Test_runTest
  (JNIEnv *, jclass);

#ifdef __cplusplus
}
#endif
#endif

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 libtest.so

Finally let the example run:

java Test

Finally some links:

-- ChristianS - 20 Apr 2004


Topic HandlingJNI . { View | Diffs | r1.1 | More }
Revision -
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.