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.classNow generate Header:
javah Test --> Test.hThis 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.soFinally let the example run:
java TestFinally some links:
| Topic HandlingJNI . { Edit | Attach | Ref-By | Printable | Diffs | r1.1 | More } |
|
Revision r1.1 - 20 Apr 2004 - 04:38 GMT - ChristianS Parents: WebHome > CurrentDiscussions |
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. |