我正在练习JNI并创建了共享库文件Samplelib.so。我将创建库文件的目录添加到java.library.path中,当我运行java文件时,我得到了java.lang.UnsatisfiedLinkError。这是我的Sample.java。
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 /.../ include -I /.../ include / linux -o libSample.so Sample.c
答案 0 :(得分:3)
使用
System.loadLibrary("Samplelib");
而不是
System.load("Samplelib");