2016-06-21 85 views
0

我正在嘗試使用JNI從Java類訪問C++方法。我能夠編譯(無論是在Eclipse或者命令行)我的Java類很好,但在運行時執行類,我越來越:在運行時獲取java.lang.UnsatisfiedLinkError

Exception in thread "main" java.lang.UnsatisfiedLinkError: com.domain.services.CallServiceAPIS.createSession()I at com.domain.services.CallServiceAPIS.createSession(Native Method) at com.domain.services.CallServiceAPIS.main(CallServiceAPIS.java:18)

Java代碼如下:

package com.domain.services; 

public class CallServiceAPIS { 

    static { 
     System.loadLibrary("service.client"); 
    } 

    public native int createSession(); 

    public static void main(String[] args) { 
     System.out.println(System.getProperty("java.library.path")); 
     new CallServiceAPIS().createSession(); 
    } 
} 

我包含了java.library.path的打印輸出,以確保它指向C++庫的正確位置 - 它是。我也嘗試在Eclipse環境中設置LD_LIBRARY_PATH。但都沒有奏效。

請注意,System.loadLibrary調用IS正在工作,因爲1)代碼編譯和2)錯誤發生在第18行,這是新的CallServiceAPIs調用。

C++代碼:

int createSession(const PosServiceInfo info, const SessionArgs& args, Domain::UUID& uuidSession) 
    { 
     return int::undefined; 
    } 

任何想法?

+0

顯示定義了createSession的C++源代碼。 – gudok

+0

@gudok添加了C++源代碼 – user3712321

+0

'return int :: undefined;' - 這不是C++。 – PaulMcKenzie

回答

0

沒關係。我意識到我錯誤地使用了JNI接口。我以爲你可以使用EXISTING C++源碼加載一個EXISTING C++庫。但是你基本上必須重寫現有的代碼才能使用JNI接口。

+0

使用jna代替原始jni可能更容易(以性能損失爲代價)。 – gudok

+0

@gudok謝謝,會嘗試。 – user3712321