当前位置: 代码迷 >> Java Web开发 >> servlet调用JNI(返回值是结构体)有关问题
  详细解决方案

servlet调用JNI(返回值是结构体)有关问题

热度:281   发布时间:2016-04-17 00:54:25.0
servlet调用JNI(返回值是结构体)问题
如题,我在servlet中调用jni发生异常,jni返回一个结构体。总是在返回结构体的时候就异常了。
代码如下:
结构体代码:
Java code
package Moniter.dictionary;public class mystruct {    public int i1;    public int i2;}

jni 的代码:
Java code
package Moniter.dictionary;public class TestStruct {    static {        System.loadLibrary("TestJni");    }    public static native mystruct getMystruct(); }

Jni 调用DLL的部分:生成的头文件:
C/C++ code
/* DO NOT EDIT THIS FILE - it is machine generated */#include <jni.h>/* Header for class Moniter_dictionary_TestStruct */#ifndef _Included_Moniter_dictionary_TestStruct#define _Included_Moniter_dictionary_TestStruct#ifdef __cplusplusextern "C" {#endif/* * Class:     Moniter_dictionary_TestStruct * Method:    getMystruct * Signature: ()LMoniter/dictionary/mystruct; */JNIEXPORT jobject JNICALL Java_Moniter_dictionary_TestStruct_getMystruct  (JNIEnv *, jclass);#ifdef __cplusplus}#endif#endif

jni CPP:
C/C++ code
#include "stdafx.h"#include "Moniter_dictionary_TestStruct.h"JNIEXPORT jobject JNICALL Java_Moniter_dictionary_TestStruct_getMystruct(JNIEnv *env, jclass obj){    /**//* 下面为获取到Java中对应的实例类中的变量*/     OutputDebugString(("******0 setp"));    jclass objectClass = (env)->FindClass("Moniter/dictionary/mystruct");      if (objectClass == NULL)    {        OutputDebugString(("******the object is null"));    }    //获取类中每一个变量的定义      //名字     //序列号      OutputDebugString(("******2 setp"));    jfieldID ival1 = (env)->GetFieldID(objectClass,"i1","I");    jfieldID ival2 = (env)->GetFieldID(objectClass,"i2","I");      OutputDebugString(("******3 setp"));    //给每一个实例的变量付值      (env)->SetShortField(obj,ival1,10);     (env)->SetShortField(obj,ival2,10);    OutputDebugString(("******5 setp"));    return obj;  }


在servlet中调用
Java code
mystruct ttMystruct = TestStruct.getMystruct();

从调用testjni.dll的 outputdebug日志来看都是执行到("******5 setp")后才异常的所以才断定是返回的时候异常。求教有谁遇到过返回结构体的时候异常的同志哥给下原因和解决方法,我试过如果返回结构体是没有问题的。

------解决方案--------------------
你这到底是java啊还是C++啊
结构体,好头疼的东西啊
------解决方案--------------------
JNIEXPORT jobject JNICALL Java_Moniter_dictionary_TestStruct_getMystruct(JNIEnv *env, jclass obj)


jclass objectClass = (env)->FindClass("Moniter/dictionary/mystruct");

jclass obj应该不是一个实例吧是一个Class类, 你应该使用env new一个对象吧。
  相关解决方案