help please! for attempt at JNI

Discussion in 'Java' started by Guest, Aug 26, 2003.

  1. Guest

    Guest 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
     
    Guest, Aug 26, 2003
    #1
    1. Advertising

  2. Guest

    Guest Guest

    Fixed it: Re: help please! for attempt at JNI

    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



    <h113862@_NOSPAM_hotmail.com> wrote in message
    news:vkQ2b.128077$...
    > 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
    >
    >
     
    Guest, Aug 26, 2003
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Tiraman
    Replies:
    0
    Views:
    823
    Tiraman
    May 10, 2004
  2. Alex Hunsley

    IBM's JNI fails where Sun's JNI works

    Alex Hunsley, Nov 3, 2003, in forum: Java
    Replies:
    4
    Views:
    861
    Alex Hunsley
    Nov 4, 2003
  3. KK
    Replies:
    2
    Views:
    646
    Big Brian
    Oct 14, 2003
  4. GMI
    Replies:
    3
    Views:
    516
    Tad McClellan
    Jun 19, 2005
  5. Lae.
    Replies:
    7
    Views:
    102
    Dennis Ă…lund
    Dec 11, 2005
Loading...

Share This Page