1
我正在練習JNI並創建共享庫文件Samplelib.so。我將創建庫文件的目錄添加到java.library.path中,當我運行java文件時,我得到java.lang.UnsatisfiedLinkError。這是我的Sample.java。JNI不滿意鏈接錯誤?
import java.util.*;
public class Sample{
public native int intmethod(int n);
public native String stringmethod(String s);
public static void main(String[] args) {
try{
//System.setProperty("java.library.path", "/home/sudhagar/Project");
System.load("libSample");
Sample sample=new Sample();
int sq=sample.intmethod(2);
String text=sample.stringmethod("JAVA");
System.out.println(sq);
System.out.println(text);
}
catch(UnsatisfiedLinkError e){
String property = System.getProperty("java.library.path");
StringTokenizer parser = new StringTokenizer(property, ";");
while (parser.hasMoreTokens()) {
System.err.println(parser.nextToken());
}
}
}
}
我sample.c文件文件,
#include "Sample.h"
#include <string.h>
JNIEXPORT jint JNICALL Java_Sample_intmethod
(JNIEnv *env, jobject obj, jint n){
return n*n;
}
JNIEXPORT jstring JNICALL Java_Sample_stringmethod
(JNIEnv *env, jobject obj, jstring n){
return n;
}
void main(){}
我用下面的命令來創建創建共享庫, 的gcc -shared -I /.../包括-I/... /在include/linux -o libSample.so sample.c文件