在eclipse中,在64位窗口上,我试图让一个JNI示例工作,包括尝试两种处理输入/输出int参数的方法。该程序运行并执行sayHello(),但以modIntPtr()的以下异常终止:
线程“main”中的异常java.lang.UnsatisfiedLinkError:com.aaa.bbb.ccc.HelloJNI.modIntPtr(I [I] V at com.aaa.bbb.ccc.HelloJNI.modIntPtr(Native Method) 在com.aaa.bbb.ccc.HelloJNI.main(HelloJNI.java:18)
我仔细检查了库,它里面有modIntPtr。所以我认为它一定是签名问题?在.cpp文件中,modIntPtr有一个jint,jintArray,并返回一个void。那是(我[I] V。在.h文件中,签名是(I [I] V。在HelloJNI.java中,我这样称呼它: new 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;
}
HelloJNI.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中传递输入/输出或可变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;
}
}
答案 0 :(得分:0)
发布的代码运行没有问题。我会尝试做一个干净的DLL构建; JVM必须从某个地方加载陈旧版本。
顺便说一下,Java_com_aaa_bbb_ccc_HelloJNI_modIntPtr
似乎有一个错误:
printf("intptr = %d\n",intptr[0]);
可能应该是:
printf("intptr = %d\n",body[0]);