当前位置:   article > 正文

java使用jni连接tuxedo(linux)_java通过jni调用tuxedo

java通过jni调用tuxedo

运行环境

本文章都是在Linux环境,redhat下操作。
我使用的环境:
java环境:JDK 1.8
tuxedo客户端:tuxedo121300_64_Linux_01_x86.zip。下载地址:Oracle Tuxedo Downloads。。
C语言拼包,java发起交易。

1、java程序

新建TuxedoJNI.java文件

public class TuxedoJNI{
        static{
                System.load("/home/test/tuxedo/libtuxedo.so");
        }
        public native static int tuxedo_send();

        public static void main(String[] args)
        {
                TuxedoJNI test = new TuxedoJNI();
                test.tuxedo_send();
        }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

编译java程序,生成TuxedoJNI.class

javac TuxedoJNI.java
  • 1

2、生成C的头文件

使用javah命令,生成TuxedoJNI.h头文件,

当前目录下使用javah报错,找不到’TuxedoJNI’的类文件,可以加入-classpath ./指定当前目录。

javah -classpath ./ TuxedoJNI
  • 1

生成的TuxedoJNI.h

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

#ifndef _Included_TuxedoJNI
#define _Included_TuxedoJNI
#ifdef __cplusplus
extern "C" {
#endif
/*

 * Class:     TuxedoJNI
 * Method:    tuxedo_send
 * Signature: ()I
   */
   JNIEXPORT jint JNICALL Java_TuxedoJNI_tuxedo_1send
     (JNIEnv *, jclass);

#ifdef __cplusplus
}
#endif
#endif
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

3、C程序

新建client.c

#include <stdio.h>
#include "atmi.h"               /* TUXEDO  Header File */
#include "TuxedoJNI.h"
#define DATALEN 2048
/*void main() */
JNIEXPORT jint JNICALL
Java_TuxedoJNI_tuxedo_1send(JNIEnv *env, jclass jc)
{
        char *sendbuf, *rcvbuf;
        long sendlen, rcvlen;
        int ret,iLen=0,lLen=0;

        putenv("WSNADDR=//192.168.1.11:8000");
        /* Attach to System/T as a Client Process */
        if (tpinit((TPINIT *) NULL) == -1) {
                (void) fprintf(stderr, "Tpinit failed\n");
                exit(1);
        }
    
        /* Allocate STRING buffers for the request and the reply */
    
        if((sendbuf = (char *) tpalloc("CARRAY", NULL, DATALEN+1)) == NULL) {
                (void) fprintf(stderr,"Error allocating send buffer\n");
                tpterm();
                exit(1);
        }
    
        if((rcvbuf = (char *) tpalloc("CARRAY", NULL, DATALEN+1)) == NULL) {
                (void) fprintf(stderr,"Error allocating receive buffer\n");
                tpfree(sendbuf);
                tpterm();
                exit(1);
        }
    
        /* orgnize the package */
    	sendlen = Pack_buf(sendbuf);//发包自己实现

        /* Request the service IPPSRV, waiting for a reply */
        ret = tpcall("zhcx", (char *)sendbuf, sendlen, (char **)&rcvbuf, &rcvlen, (long)0);
        if(ret == -1 && tperrno==TPESYSTEM) {
                tpterm();
                ret = tpcall("zhcx", (char *)sendbuf, sendlen, (char **)&rcvbuf, &rcvlen, (long)0);
        }
    
        if(ret == -1) {
                (void) fprintf(stderr, "Can't send request to service IPPSRV\n");
                (void) fprintf(stderr, "Tperrno = %d\n", tperrno);
                tpfree(sendbuf);
                tpfree(rcvbuf);
                tpterm();
                exit(1);
        }
        unPack_buf(rcvbuf,rcvlen); //收包自己实现

        /* Free Buffers & Detach from System/T */
        tpfree(sendbuf);
        tpfree(rcvbuf);
        tpterm();

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60

使用makefile编译,生成libtuxedo.so

TUXDIR=/home/tuxedo/OraHome_1/tuxedo12.1.3.0.0
JNIINC=/home/ipp/test/jdk1.8.0_271/include
JNIMDINC=/home/ipp/test/jdk1.8.0_271/include/linux

CC      =  cc
CLIB    = -lx
#
# 本项目的所有模块
#
PROJECT = tuxedo
#
# 所有编译单元
#
all:tuxedo

tuxedo:cli.c
        buildclient -w -o libtuxedo.so -f " -I$(JNIINC) -I$(JNIMDINC) -fPIC -shared cli.c pack.c "  -l "-lc"
clean:
        -rm libtuxedo.so
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

4、执行程序测试

java -classpath ./ TuxedoJNI

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小小林熬夜学编程/article/detail/371821
推荐阅读
相关标签
  

闽ICP备14008679号