Reputation: 188
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());
}
}
}
}
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(){}
I used following command to create the create the shared library, gcc -shared -I/.../include -I/.../include/linux -o libSample.so Sample.c
Upvotes: 1
Views: 1799
Reputation: 2846
Use
System.loadLibrary("Samplelib");
instead of
System.load("Samplelib");
Upvotes: 3