在eclipse中,在64位Windows上,我试图使JNI示例正常工作,包括尝试两种处理in / out int参数的方法。该程序运行并执行sayHello(),但由于modIntPtr()的以下异常而终止:

线程“主”中的异常java.lang.UnsatisfiedLinkError:com.aaa.bbb.ccc.HelloJNI.modIntPtr(I [I)V
在com.aaa.bbb.ccc.HelloJNI.modIntPtr(本机方法)
在com.aaa.bbb.ccc.HelloJNI.main(HelloJNI.java:18)

我仔细检查了该库,其中确实包含modIntPtr。所以我想这一定是签名问题吗?在.cpp文件中,modIntPtr具有一个jint,jintArray,并返回一个void。那是(I [I] V。在.h文件中,签名为(I [I)V。在HelloJNI.java中,我这样称呼它:
新的HelloJNI()。modIntPtr(100,intptr);
intptr是int []类型。所以也就是(I [I] V)。

因此,如果.dll包含modIntPtr方法,则我确定它是最新的,我确定程序正在与正确的.dll链接,并且这不是签名问题,还可能是什么?

顺便说一句,如果我注释掉对modIntPtr的调用,modIntRef确实可以工作。

谢谢你的帮助!

HelloJNI.java:

package com.aaa.bbb.ccc;

public class HelloJNI {
    static {
       System.loadLibrary("com_aaa_bbb_ccc");
    }
    // A native method that receives nothing and returns void
    private native void sayHello();
    private native void modIntPtr(int delta, int[] intptr);
    private native void modIntRef(int delta, IntRef intref);

    public static void main(String[] args) {
       int[] intptr = new int[1];
       intptr[0]=42;
       IntRef intref = new IntRef(24);

       new HelloJNI().sayHello();  // invoke the native method
       new HelloJNI().modIntPtr(100, intptr);
       new HelloJNI().modIntRef(100, intref);
    }
 }

HelloJNI.cpp
#include <jni.h>
#include <stdio.h>
#include "HelloJNI.h"

void SayHello(void)
{
   printf("Hello World!\n");
   return;
}

void IntRefNative(int delta, int *intptr)
{
   printf("delta  = %d\n",delta);
   printf("intptr = %d\n",*intptr);

   *intptr += delta;

   printf("After mod...\n");
   printf("intptr = %d\n",*intptr);

   return;
}


JNIEXPORT void JNICALL Java_com_aaa_bbb_ccc_HelloJNI_sayHello(JNIEnv *env, jobject thisObj) {
   SayHello();
   return;
}

JNIEXPORT void JNICALL Java_com_aaa_bbb_ccc_HelloJNI_modIntPtr(JNIEnv *env, jobject thisObj, jint delta, jintArray intptr) {
   jint *body = env->GetIntArrayElements(intptr, 0);
   printf("delta  = %d\n",delta);
   printf("intptr = %d\n",intptr[0]);

   body[0] += delta;
   env->ReleaseIntArrayElements(intptr, body, 0);

   printf("After mod...\n");
   printf("intptr = %d\n",intptr[0]);
   return;
}

JNIEXPORT void JNICALL Java_com_aaa_bbb_ccc_HelloJNI_modIntRef(JNIEnv *env, jobject thisObj, jint delta, jobject intref) {
    jclass cls = env->GetObjectClass(intref);

    jmethodID methodID = env->GetMethodID(cls, "getValue", "()I");
    jint value = env->CallIntMethod(intref, methodID);
    IntRefNative(delta, (int *)(&value));
    methodID = env->GetMethodID(cls, "setValue", "(I)V");
    env->CallVoidMethod(intref, methodID, value);

   return;
}

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

#ifndef _Included_com_aaa_bbb_ccc_HelloJNI
#define _Included_com_aaa_bbb_ccc_HelloJNI
#ifdef __cplusplus
extern "C" {
#endif
/*
 * Class:     com_aaa_bbb_ccc_HelloJNI
 * Method:    sayHello
 * Signature: ()V
 */
JNIEXPORT void JNICALL Java_com_aaa_bbb_ccc_HelloJNI_sayHello
  (JNIEnv *, jobject);

/*
 * Class:     com_aaa_bbb_ccc_HelloJNI
 * Method:    modIntPtr
 * Signature: (I[I)V
 */
JNIEXPORT void JNICALL Java_com_aaa_bbb_ccc_HelloJNI_modIntPtr
  (JNIEnv *, jobject, jint, jintArray);

/*
 * Class:     com_aaa_bbb_ccc_HelloJNI
 * Method:    modIntRef
 * Signature: (ILcom/aaa/bbb/ccc/IntRef;)V
 */
JNIEXPORT void JNICALL Java_com_aaa_bbb_ccc_HelloJNI_modIntRef
  (JNIEnv *, jobject, jint, jobject);

#ifdef __cplusplus
}
#endif
#endif

对于试图在Java中传递in / out或可变的int参数的想法的人,我也将包括该文件...我到处搜索了一个工作示例。
IntRef.java
package com.aaa.bbb.ccc;

public class IntRef {

    private int value;

    IntRef() {
        value=0;
    }

    IntRef(int v) {
        value = v;
    }

    /**
     * Returns the integer value of this <code>IntRef</code>
     *
     * @return the value of the integer in native int form
     */
    public int getValue() {
        return value;
    }

    /**
     * Sets the value.
     *
     * @param theValue The value to set
     */
    public void setValue(int theValue) {
        this.value = theValue;
    }
}

最佳答案

所发布的代码可以正常运行。我将尝试对DLL进行干净的构建。 JVM必须从某个地方加载旧版本。

顺便说一句,Java_com_aaa_bbb_ccc_HelloJNI_modIntPtr中似乎有一个错误:

printf("intptr = %d\n",intptr[0]);

可能应该是:
printf("intptr = %d\n",body[0]);

10-06 11:06