本文已参与「新人创作礼」活动,一起开启掘金创作之路。
运行环境
本文章都是在Windows环境,win10专业版下操作。 我使用的环境: java环境:JDK 1.8 tuxedo客户端:tuxedo121300_64_win_2k8_x86_VS2012.zip 。下载地址:Oracle Tuxedo Downloads。
Visual Studio 2012:itellyou下载
C语言拼包,java的main函数发起交易。
1、java程序
新建TuxedoJNI.java文件
package cn.sunline.ltts.busi.apbase.tools;
public class TuxedoJNI{
static{
System.load("client");
}
public native static int tuxedo_send();
public static void main(String[] args)
{
TuxedoJNI test = new TuxedoJNI();
test.tuxedo_send();
}
}
编译java程序,CMD进入java文件所在文件夹,生成TuxedoJNI.class
javac TuxedoJNI.java
2、生成C的头文件
使用javah命令,生成TuxedoJNI.h头文件,
找不到TuxedoJNI类时,类前面,应该加入package的目录
CMD进入项目目录。\src\main\java目录中(根据自己目录选择)
javah -d ./ cn.sunline.ltts.busi.apbase.tools.TuxedoJNI
生成的cn_sunline_ltts_busi_apbase_tools_TuxedoJNI_SendRecvEx.h
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class cn_sunline_ltts_busi_apbase_tools_TuxedoJNI */
#ifndef _Included_cn_sunline_ltts_busi_apbase_tools_TuxedoJNI
#define _Included_cn_sunline_ltts_busi_apbase_tools_TuxedoJNI
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: cn_sunline_ltts_busi_apbase_tools_TuxedoJNI
* Method: SendRecvEx
* Signature: ()I
*/
JNIEXPORT jint JNICALL Java_cn_sunline_ltts_busi_apbase_tools_TuxedoJNI_SendRecvEx
(JNIEnv *, jclass);
#ifdef __cplusplus
}
#endif
#endif
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_cn_sunline_ltts_busi_apbase_tools_TuxedoJNI_SendRecvEx(JNIEnv *env, jclass jc)
{
char *sendbuf, *rcvbuf;
long sendlen, rcvlen;
int ret,iLen=0,lLen=0;
tuxputenv("WSNADDR=//192.168.1.11:8000"); //tuxputenv tuxedo windows设置环境变量使用
/* 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();
}
设置环境变量,windows设置环境变量方案,请使用系统属性那块设置。为记录方便使用set。
set JAVA_HOME=C:\Program Files\Java\jdk1.8.0_221;D:\programe\tuxedo\tuxedo12.1.3.0.0_VS2012\jre;
set PATH=D:\programe\tuxedo\tuxedo12.1.3.0.0_VS2012\bin;C:\Program Files\Java\jdk1.8.0_221\bin;C:\Program Files\Java\jre1.8.0_221\bin;D:\programe\tuxedo\tuxedo12.1.3.0.0_VS2012\jre\bin;C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\bin;C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE;
set TUXDIR=D:\programe\tuxedo\tuxedo12.1.3.0.0_VS2012;
set INCLUDE=C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\include;C:\bea\tuxedo11gR1_VS2010\include;C:\Program Files\Java\jdk1.8.0_221\include;C:\Program Files\Java\jdk1.8.0_221\include\win32;
set LIB=%TUXDIR%\lib;C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\lib;C:\Program Files (x86)\Windows Kits\8.0\Lib\win8\um\x86;D:\htw\tuxedo;
使用buildclient调用cl.exe编译器编译,生成client.dll,把C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\bin\vcvars64.bat放到当前目录。使用cmd,生产dll。
call vcvars64.bat
buildclinet -w -o cli.dll -f "-LD cli.c " -v
4、执行程序测试
使用eclipse执行,run按钮执行。