Exception in thread "main" java.lang.UnsatisfiedLinkError: Fileinfo.seturl(Ljava/lang/String;)V
at Fileinfo.seturl(Native Method)
at TestMain.main(TestMain.java:3)
java代码1public class Fileinfo{
static{
System.loadLibrary("fileInfo");
}
public static  native void seturl(String url);
public static native String getctime();
}java测试main方法代码
public class TestMain{
public static void main(String agrs[]){
Fileinfo.seturl("aaaa");
System.out.println(Fileinfo.getctime());
}

}
javah生成头文件代码
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class Fileinfo */#ifndef _Included_Fileinfo
#define _Included_Fileinfo
#ifdef __cplusplus
extern "C" {
#endif
/*
 * Class:     Fileinfo
 * Method:    seturl
 * Signature: (Ljava/lang/String;)V
 */
JNIEXPORT void JNICALL Java_Fileinfo_seturl
  (JNIEnv *, jclass, jstring);/*
 * Class:     Fileinfo
 * Method:    getctime
 * Signature: ()Ljava/lang/String;
 */
JNIEXPORT jstring JNICALL Java_Fileinfo_getctime
  (JNIEnv *, jclass);#ifdef __cplusplus
}
#endif
#endifC+代码#include "Fileinfo.h"
#include <malloc.h>
#include <string.h>
#include <memory.h>
char* jstringTostring(JNIEnv* env, jstring jstr)
{
    char* rtn = NULL;
    jclass clsstring = env->FindClass("java/lang/String");
    jstring strencode = env->NewStringUTF("utf-8");
    jmethodID mid = env->GetMethodID(clsstring, "getBytes", "(Ljava/lang/String;)[B");
    jbyteArray barr= (jbyteArray)env->CallObjectMethod(jstr, mid, strencode);
    jsize alen = env->GetArrayLength(barr);
    jbyte* ba = env->GetByteArrayElements(barr, JNI_FALSE);
    if (alen > 0)
    {
        rtn = (char*)malloc(alen + 1);    
        memcpy(rtn, ba, alen);
        rtn[alen] = 0;
    }
    env->ReleaseByteArrayElements(barr, ba, 0);
    return rtn;
}
jstring stoJstring(JNIEnv* env, const char* pat)
{
    jclass strClass = env->FindClass("Ljava/lang/String;");
    jmethodID ctorID = env->GetMethodID(strClass, "<init>", "([BLjava/lang/String;)V");
    jbyteArray bytes = env->NewByteArray(strlen(pat));
    env->SetByteArrayRegion(bytes, 0, strlen(pat), (jbyte*)pat);
    jstring encoding = env->NewStringUTF("utf-8");
    return (jstring)env->NewObject(strClass, ctorID, bytes, encoding);
}
char *url="";
/*
 * Class:     Fileinfo
 * Method:    seturl
 * Signature: (Ljava/lang/String;)V
 */
JNIEXPORT void JNICALL Java_Fileinfo_seturl
  (JNIEnv * env, jclass, jstring jst){
   url=jstringTostring(env,jst);
}/*
 * Class:     Fileinfo
 * Method:    getctime
 * Signature: ()Ljava/lang/String;
 */
JNIEXPORT jstring JNICALL Java_Fileinfo_getctime
  (JNIEnv * env, jclass){
        return   stoJstring(env,url);        
}

解决方案 »

  1.   

    Thrown if the Java Virtual Machine cannot find an appropriate native-language definition of a method declared native. 没见有seturl方法的实现
      

  2.   

    /*
     * Class:     Fileinfo
     * Method:    seturl
     * Signature: (Ljava/lang/String;)V
     */
    JNIEXPORT void JNICALL Java_Fileinfo_seturl
      (JNIEnv * env, jclass, jstring jst){
       url=jstringTostring(env,jst);
    }这个不是seturl的实现么?
      

  3.   

    C++作成的dll是否放在了classpath路径下?
      

  4.   

    我放在classpath 和 .class文件下都是出这个异常= =
      

  5.   

    首先确定下是不是因为library找不到而引起的,如果不是就可以缩小范围了loadLibrary会抛很多异常的Throws: 
    SecurityException - if a security manager exists and its checkLink method doesn't allow loading of the specified dynamic library 
    UnsatisfiedLinkError - if the library does not exist. 
    NullPointerException - if libname is null