最近在写个WEB项目前台采用JAVA(jsp+struts+javabean+tomcat),后台用C++写业务逻辑。通过JNI调用C++,现在出现的问题是我在传byte[]数组的时候传到C++中却为空,也没有报错,用ExceptionCheck()函数扑捉也没扑捉到异常。下面是代码:
JAVACALL代码:package com.bid.call;
public class JNICall {
static{
  System.loadLibrary("Service");
}
public native byte[] ServerProcess(byte[] strBuffer, int bufferLen);
}C++jni接口代码:
com_bid_call_JNICall.h:/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
#include "BaseCbo.h"
/* Header for class com_bid_unicall_JNICall */#ifndef _Included_com_bid_call_JNICall
#define _Included_com_bid_call_JNICall
#ifdef __cplusplus
extern "C" {
#endif
/*
 * Class: com_bid_call_JNICall
 * Method: ServerProcess
 * Signature: ([BI)[B
 */
JNIEXPORT jbyteArray JNICALL Java_com_bid_call_JNICall_ServerProcess
  (JNIEnv *, jobject, jbyteArray, jint);int ParamBuff2ObjConvert(CBaseCbo& oCParamPkg, char *strBuffer, int nLen);int ParamObj2BuffConvert(CBaseCbo& oCParamPkg, char **strBuffer, int *nLen);#ifdef __cplusplus
}
#endif
#endif
com_bid_call_JNICall.cpp:#include "ParamPkg.h"
#include "BusinessDispatch.h"
#include "com_bid_call_JNICall.h"
JNIEXPORT jbyteArray JNICALL Java_com_bid_call_JNICall_ServerProcess
  (JNIEnv *env, jobject obj, jbyteArray jBuffer, jint jLen)
{
CParamPkg objParamPkg;
CBusinessDispatch BusinessDispatch;char *strRetBuffer = NULL;
int nRetLen = 0;char *strBuffer = (char *)env->GetByteArrayElements(jBuffer, 0);/* 这里取得的是空,在JAVA中已经确认非空 */
if (env->ExceptionCheck() == JNI_TRUE || strBuffer == NULL)
  {
  env->ExceptionDescribe();
  env->ExceptionClear();
  }/* JParamPack convert to CParamPack */
ParamBuff2ObjConvert(objParamPkg, strBuffer, jLen);/* 调用分发对象 进行分发处理 */
BusinessDispatch.DispatchProcess(objParamPkg);/* CParamPack convert to JParamPack */
ParamObj2BuffConvert(objParamPkg, &strRetBuffer, &nRetLen);/* 将字符流变回成jbyteArray返回Jvm */
env->SetByteArrayRegion(jBuffer,0,nRetLen,(jbyte *)strRetBuffer);  delete strRetBuffer;return jBuffer;
}上面是JNI接口的代码,在JAVA中已经调试到ServerProcess(byte[] strBuffer, int bufferLen)函数,而且已经确认了strBuffer是有值的,但是当我调试DLL,跟踪到char *strBuffer = (char *)env->GetByteArrayElements(jBuffer, 0);之后
strBuffer却为“”,里面没有任何值。也没异常。(jint jLen 参数传递过来了,值也正常)
刚用JNI不熟悉,麻烦哪位大哥点解下,不甚感激。不知道是不是我哪里的路径没设置对,路径问题搞的我晕晕的