這個問題比較急,借用首頁發一下,謝謝!
最近需要對一個C++庫進行封裝,為簡單起見決定使用JAVA對其封裝。但是一直調用不成功,于是寫了一段測試代碼測試一下。為簡單起見,就使用默認包了。
public class TestDLL {
public native String Test(String put);
static{
System.loadLibrary("testdll");
}
public static void main(String[] args) {
TestDLL test = new TestDLL();
System.out.println(test.Test(new Date().toString()));
}
}
然后生成了C++頭文件
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class TestDLL */
#ifndef _Included_TestDLL
#define _Included_TestDLL
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: TestDLL
* Method: Test
* Signature: (Ljava/lang/String;)Ljava/lang/String;
*/
JNIEXPORT jstring JNICALL Java_TestDLL_Test
(JNIEnv *, jobject, jstring);
#ifdef __cplusplus
}
#endif
#endif
下面是簡單的實現
#include "TestDLL.h"
JNIEXPORT jstring JNICALL Java_TestDLL_Test
(JNIEnv *, jobject, jstring put){
return put;
}
在linux下通過gcc編譯后,java調用:
java -Djava.library.path=/home/xp -cp . TestDLL
一直報錯:
Exception in thread "main" java.lang.UnsatisfiedLinkError: no testdll in java.library.path
at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1734)
at java.lang.Runtime.loadLibrary0(Runtime.java:823)
at java.lang.System.loadLibrary(System.java:1028)
at TestDLL.main(TestDLL.java:27)
但是我在windows下編譯了testdll.dll后,能夠調用正常。
接著我改寫為System.load("/home/xp/testdll.so");結果可以在linux使用了,希望有經驗的朋友可以解答我的疑問。