2014-09-02 38 views
1

這已被問了幾次,並有不同的接受答案。但他們都沒有爲我工作。
我從我的項目中製作了一個jni dll,用於使用eclipse和jdk1.7.0_10(64位)的64x windows 7。但加載我的DLL後,我得到java.lang.UnsatisfiedLinkError。
我開始創建一個基於這個guide的helloworld項目。我做它所說的一切。但我已經收到此錯誤:JNI:得到java.lang.UnsatisfiedLinkError

Exception in thread "main" java.lang.UnsatisfiedLinkError: test.HelloWorld.print()V 
    at test.HelloWorld.print(Native Method) 
    at test.HelloWorld.main(HelloWorld.java:24) 

是的,我已經包括在庫路徑是的,我構建C項目64是的,我使用64位JVM。

代碼:

package test; 

public class HelloWorld { 
    public native void print(); //native method 
    static //static initializer code 
    { 
     try{ 
      System.loadLibrary("CLibHelloWorld"); 

       } 
       catch (UnsatisfiedLinkError e) { 
        System.err.println("Native code library failed to load.\n" + e); 
        System.exit(1); 
       } 

    } 

    public static void main(String[] args) 
    { 
     HelloWorld hw = new HelloWorld(); 
     hw.print();    // ==> i get error on this line 
    } 
} 

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

#ifndef _Included_HelloWorld 
#define _Included_HelloWorld 
#ifdef __cplusplus 
extern "C" { 
#endif 
/* 
* Class:  HelloWorld 
* Method: print 
* Signature:()V 
*/ 
JNIEXPORT void JNICALL Java_HelloWorld_print 
    (JNIEnv *, jobject); 

#ifdef __cplusplus 
} 
#endif 
#endif 

#include "HelloWorld.h" 
#include "jni.h" 
#include "stdio.h" 

JNIEXPORT void JNICALL Java_HelloWorld_print(JNIEnv *env, jobject obj) 
{ 
    printf("Hello world\n"); 
    return; 
} 
+1

出於好奇,你的包名稱,因爲它可以幫助您調試問題,如果你用[JavaCPP]嘗試(HTTPS:/ /github.com/bytedeco/javacpp),它工作嗎? – 2014-09-02 07:48:59

+0

@SamuelAudet我會試試 – osyan 2014-09-02 07:52:19

回答

2

test.HelloWorld.print C名字是

Java_test_HelloWorld_print 

您錯過了_test - 您是否在默認包中運行了javah類?

+1

謝謝。有效。 +1運行類從默認包 – osyan 2014-09-02 08:12:34

+0

在我的項目中,我有像這樣的功能:** Java_Secure_Key_connect ** 所以在Java中我已經創建了項目命名**測試**然後一個名爲** Secure **與一個名爲* *鍵**,並在該類中調用本機**連接**功能。應該這樣做還是我犯了錯誤? – osyan 2014-09-02 08:24:49

+0

我認爲名稱是區分大小寫的,所以這個類需要'Key',否則我認爲這是好的。 – 2014-09-02 08:40:34

0

你錯過了包名測試 你應該包括在C

相關問題