JNI不满意链接错误? [英] JNI unsatisfiedLinkError?
本文介绍了JNI不满意链接错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在练习JNI并创建了共享库文件Samplelib.so。我将创建库文件的目录添加到java.library.path中,当我运行java文件时,我得到了java.lang.UnsatisfiedLinkError。这是我的Sample.java。
I am practising JNI and created the shared library file Samplelib.so. I added the directory in which the library file is created, to the java.library.path and when I run the java file, I get java.lang.UnsatisfiedLinkError. Here is my 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文件,
My Sample.c file,
#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
I used following command to create the create the shared library, gcc -shared -I/.../include -I/.../include/linux -o libSample.so Sample.c
推荐答案
使用
System.loadLibrary("Samplelib");
而不是
System.load("Samplelib");
这篇关于JNI不满意链接错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文