环境:Suse 9 Tuxedo 8.1 Java1.4 $JAVA_HOME $TUXDIR
实现的步骤
编写带native方法的Java类/**
* This is a demo class for test JNI
*
*
*/
public class Hello {
static {
try {
// "hello" means the library of native method
System.loadLibrary("hello");
} catch (UnsatisfiedLinkError e) {
System.err.println("Cannot load hello library:“n " + e.toString());
}
}
public Hello() {
}
// This is a native method without args and return
public native void SayHello();
}
2.编译为.class文件
javac Hello.java
3.生成.h文件
javah Hello|||
4.编写对应的C文件,我一般直接拷贝一个.h文件为.c,之后修改,感觉比较不会出错
#include <stdio.h>
#include "atmi.h" /* TUXEDO Header File */
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class Hello */
#ifndef _Included_Hello
#define _Included_Hello
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: Hello
* Method: SayHello
* Signature: ()V
*/
JNIEXPORT void JNICALL Java_Hello_SayHello
(JNIEnv * env, jobject arg)
{
char *sendbuf, *rcvbuf;
long sendlen, rcvlen;
int ret;
printf("THIS IS JNI Say Hello“n");
/* Attach to System/T as a Client Process */
if (tpinit((TPINIT *) NULL) == -1) {
(void) fprintf(stderr, "Tpinit failed“n");
exit(1);
}
sendlen = 32;
/* Allocate STRING buffers for the request and the reply */
if((sendbuf = (char *) tpalloc("STRING", NULL, sendlen+1)) == NULL) {
(void) fprintf(stderr,"Error allocating send buffer“n");
tpterm();
exit(1);
}|||
if((rcvbuf = (char *) tpalloc("STRING", NULL, sendlen+1)) == NULL) {
(void) fprintf(stderr,"Error allocating receive buffer“n");
tpfree(sendbuf);
tpterm();
exit(1);
}
(void) strcpy(sendbuf, "0123456789abcdef0123456789abcdef");
/* Request the service TOUPPER, waiting for a reply */
ret = tpcall("TOUPPER", (char *)sendbuf, 0, (char **)&rcvbuf, &rcvlen, (long)0);
if(ret == -1) {
(void) fprintf(stderr, "Can‘t send request to service TOUPPER“n");
(void) fprintf(stderr, "Tperrno = %d“n", tperrno);
tpfree(sendbuf);
tpfree(rcvbuf);
tpterm();
exit(1);
}
(void) fprintf(stdout, "Returned string is: %s“n", rcvbuf);
/* Free Buffers & Detach from System/T */
tpfree(sendbuf);
tpfree(rcvbuf);
tpterm();
return;
}
#ifdef __cplusplus
}
#endif
#endif
5.编译.c为.o .so
gcc -I$JAVA_HOME/include -I$JAVA_HOME/include/linux -I$TUXDIR/include -c -L${TUXDIR}/lib Hello.c
gcc -shared -I$JAVA_HOME/include -I$JAVA_HOME/include/linux -I$TUXDIR/include -L$TUXDIR/lib -ltux -lbuft -lfml -lfml32 -lengine -ldl -lpthread /usr/lib/libcrypt.a -Wl,-soname,libhello.so.1 -o libhello.so.1.0 Hello.o
cp libhello.so.1.0 libhello.so
6.ldd libhello.so
如果看到一下的输出,恭喜!基本OK啦!|||
linux-gate.so.1 => (0xffffe000)
libtux.so => /home/tuxedo/product/tuxedo8.1/lib/libtux.so (0x40003000)
libbuft.so => /home/tuxedo/product/tuxedo8.1/lib/libbuft.so (0x40110000)
libfml.so => /home/tuxedo/product/tuxedo8.1/lib/libfml.so (0x40112000)
libfml32.so => /home/tuxedo/product/tuxedo8.1/lib/libfml32.so (0x4012a000)
libengine.so => /home/tuxedo/product/tuxedo8.1/lib/libengine.so (0x40144000)
libdl.so.2 => /lib/libdl.so.2 (0x401db000)
libpthread.so.0 => /lib/tls/libpthread.so.0 (0x401de000)
libc.so.6 => /lib/tls/libc.so.6 (0x401ee000)
libgiconv.so => /home/tuxedo/product/tuxedo8.1/lib/libgiconv.so (0x40302000)
libusort.so => /home/tuxedo/product/tuxedo8.1/lib/libusort.so (0x403d6000)
/lib/ld-linux.so.2 => /lib/ld-linux.so.2 (0x80000000)
7.编写测试类
/**
* This class used test JNI method in Hello
*
*/
public class SayHello {
/**
* @param args
*/
public static void main(String args) {
Hello hello = new Hello();
hello.SayHello();
}
}
8.执行测试
java –cp . SayHello