help please! for attempt at JNI

G

Guest

hi all,

it just isn't working for me!

i am attempting a very simple program that calls a native method. i am
pretty sure i am following all the correct steps, but it still doesn't work.
error message is:

Exception in thread "main" java.lang.UnsatisfiedLinkError: no Native in
java.library.path
at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1403)
at java.lang.Runtime.loadLibrary0(Runtime.java:788)
at java.lang.System.loadLibrary(System.java:832)
at Native.<clinit>(Native.java:21)

step 1: write and compile the class

public class Native {
public Native() {
int x;
int y;
System.out.println("about to call calc");
x = calc(10, 30);
y = calc(11, 40);
System.out.println("x is " + x);
System.out.println("y is " + y);
}

native int calc(int x, int y);

public static void main(String[] args) {
new Native();
}
static {
System.out.println("about to load library ...");
System.out.println(System.getProperty("java.library.path"));
System.loadLibrary("Native");
System.out.println("library loaded ...");
}
}

step 2: generate the header file, create the c source, then compile and
create a shared library. I am running on redhat 9.0, so,
javah -jni Native
gcc -c Native.c
ld -shared Native.o -o Native.so

#include <jni.h>
#include "Native.h"

JNIEXPORT jint JNICALL Java_Native_calc (JNIEnv *env, jobject th, jint x,
jint y) {
jint z = x * y;
return z;
}

int main() {
printf("5 x 5 is %d", Java_Native_calc(NULL, NULL, 5, 5));
}

step 3: at this point, Native.so exists in the current directory. so run the
java program, including the current directory as the library path

java -Djava.library.path=. Native

which results in the UnsatisifiedLinkError

what am i doing wrong?



c
 
G

Guest

yay. fixed it myself.

the problem was the library's file name. to load a library called "Native",
the file libNative.so must exist in one of the directories listed in the
java.library.path


c
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,278
Latest member
BuzzDefenderpro

Latest Threads

Top