- 论坛徽章:
- 0
|
先用java 对函数进行声明 ,用javah 生成 对应的头文件
封装C语言函数包含javah生成的头文件 生成 .so ,
在要使用jni调用的java程序中
static {
System.loadLibrary("动态链接库名 ");
}
在程序中调用声明过的函数就可以了 。 回头写个例子
例子,先写个函数声明文件 EncryptUtil.java
public class EncryptUtil {
static
{
System.loadLibrary( "pin" );
}
public native static int PIN_Encrypt( String acPass, String acAcct, String P
IN_block );
public native static int PIN_Decrypt( String PIN_block, String acAcct,String
acPass );
public native static int SetParamAll(String My_IPAdress1,String My_IPAdress2
,int TimeOut);
public native static int DraftEncrypt(int TransactType,
char YwType,
char[] Date,
char[] SerialNo,
char[] Amount,
char[] SendNode,
char[] RecvNode,
}
执行 javah -jni EncryptUtil, 生成 EncryptUtil.h
-------------------------------------------------------------
/* DO NOT EDIT THIS FILE - it is machine generated */
#include
/* Header for class EncryptUtil */
#ifndef _Included_EncryptUtil
#define _Included_EncryptUtil
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: EncryptUtil
* Method: PIN_Encrypt
* Signature: (Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)I
*/
JNIEXPORT jint JNICALL Java_EncryptUtil_PIN_1Encrypt
(JNIEnv *, jclass, jstring, jstring, jstring);
/*
* Class: EncryptUtil
* Method: PIN_Decrypt
* Signature: (Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)I
*/
JNIEXPORT jint JNICALL Java_EncryptUtil_PIN_1Decrypt
(JNIEnv *, jclass, jstring, jstring, jstring);
/*
* Class: EncryptUtil
* Method: SetParamAll
* Signature: (Ljava/lang/String;Ljava/lang/String;I)I
*/
JNIEXPORT jint JNICALL Java_EncryptUtil_SetParamAll
(JNIEnv *, jclass, jstring, jstring, jint);
/*
* Class: EncryptUtil
* Method: DraftEncrypt
* Signature: (IC[C[C[C[C[C[C[C)I
*/
JNIEXPORT jint JNICALL Java_EncryptUtil_DraftEncrypt
(JNIEnv *, jclass, jint, jchar, jcharArray, jcharArray, jcharArray, jcharArray, jcharArray, jcharArray, jcharArray);
#ifdef __cplusplus
}
#endif
#endif
-----------------------------------------------------------------
写C语言函数调用的程序 jpin.c
/*
* By Vanda Group VBS2000 - 2005
*
* Functions List:
* Functions Description
*
* Editer: Mr.Dragon
* First Edit Date: 2005-08-10
*
* Update List:
* Modifier Modify Date Description
*
*/
#include
#include "d3des.h"
#include "EncryptUtil.h"
/*
* 功 能: 加密客户密码
*
* 参 数:
* 1. acPass 4-12 密码明文 IN 必须是 '0'-->'9'
* 2. acAcct 帐号 IN 开户是空,即长度为零
* 3. PIN_block 16 密码密文串 OUT
*
* 返回值: 无
*
*/
JNIEXPORT jint JNICALL Java_EncryptUtil_PIN_1Encrypt
(JNIEnv *env, jclass obj, jstring jPass, jstring jAcct, jstring jPin_block)
{
jint aa =0;
return aa;
}
这里就写了个函数,所以在jpin.exp 里定义一个就可以
生成.so makefile
.c.so:
cc -I./ -I$(JAVA_HOME)include -I$(JAVA_HOME)include/aix -o \
libpin.so -bE:jpin.exp -bnoentry -bM:SRE jpin.c
写个java 例程 调用C函数吧
package com.ncs.mif.encrypt;
import com.ncs.mif.encrypt.EncryptUtil;
public class Test {
static {
System.loadLibrary("pin");
}
/**
* @param args
*/
public native static int Hello( String acPass);
public static void main(String[] args) {
// TODO Auto-generated method stub
// MyNative a = new MyNative();
// System.out.println("Mynative is : "+a.getNameCPP());
EncryptUtil b = new EncryptUtil();
String acPass = "123456";
String acAcct = "1020701011000000573";
String PIN_block ="";
int a=b.PIN_Encrypt(acPass, acAcct, PIN_block);
System.out.println("PIN_Encrypt return : "+" PIN_block:"+a);
}
}
本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/6334/showart_287634.html |
|