IBM JVM: loading two native libraries in JNI, second one has problems

Discussion in 'Java' started by Alex Hunsley, Nov 13, 2003.

  1. Alex Hunsley

    Alex Hunsley Guest

    I've refined my problem using JNI under IBM's JVM1.4.1 (in Linux) to the
    following scenario:

    I have two java classes, X and Y, that have native methods in them. X
    and Y use different .so native library files (on a redhat linux system)
    for their native functionality.
    Now, if my main code reads as follows:

    X xInstance = new X();
    Y yInstance = new Y();

    result = yInstance.nativeMethod("a string");

    .... then I crash out with Segmentation fault/javacore dump when my
    native code for Y calls NewStringUTF or GetStringUTF. Calls to native
    code in X work though.

    If I rearrange my code as follows:

    // swapped - make a new Y *before* a new X!
    Y yInstance = new Y();
    X xInstance = new X();

    result = yInstance.nativeMethod("a string");

    ... then my native code in Y works! (but any calls to native code in X
    that use NewStringUTF or GetStringUTF are now not working.)

    So the problem appears to be that the first native library that is
    loaded works fine, any after that are broken and can't call GetStringUTF
    etc.

    I don't have these problems when using the Sun JVM, but I'm constrained
    at the minute to use the IBM JVM, so I'd like to find the problem if
    possible!

    alex


    p.s. the native library files (.so files) are loaded due to the
    following static section in classes X and Y:


    static
    {
    try
    {
    System.loadLibrary("myLibrary");
    }
    catch(UnsatisfiedLinkError unsatisfiedlinkerror)
    {
    System.err.println("myLibrary was not found.\n\n");
    unsatisfiedlinkerror.printStackTrace();
    }
    }
     
    Alex Hunsley, Nov 13, 2003
    #1
    1. Advertising

  2. Alex Hunsley

    Joel Beach Guest

    Alex Hunsley wrote:

    > I've refined my problem using JNI under IBM's JVM1.4.1 (in Linux) to the
    > following scenario:
    >


    <snip>

    >
    > p.s. the native library files (.so files) are loaded due to the
    > following static section in classes X and Y:
    >
    >
    > static
    > {
    > try
    > {
    > System.loadLibrary("myLibrary");
    > }
    > catch(UnsatisfiedLinkError unsatisfiedlinkerror)
    > {
    > System.err.println("myLibrary was not found.\n\n");
    > unsatisfiedlinkerror.printStackTrace();
    > }
    > }


    Aren't you loading the same library ("myLibrary") twice then? Once
    when class X is loaded and once when class Y is loaded. It is up
    to the programmer to ensure that a native library is only loaded
    once.

    Joel
     
    Joel Beach, Nov 13, 2003
    #2
    1. Advertising

  3. Alex Hunsley

    Joel Beach Guest

    Joel Beach wrote:

    > Alex Hunsley wrote:
    >
    >> I've refined my problem using JNI under IBM's JVM1.4.1 (in Linux) to the
    >> following scenario:
    >>

    >
    > <snip>
    >
    >>
    >> p.s. the native library files (.so files) are loaded due to the
    >> following static section in classes X and Y:
    >>
    >>
    >> static
    >> {
    >> try
    >> {
    >> System.loadLibrary("myLibrary");
    >> }
    >> catch(UnsatisfiedLinkError unsatisfiedlinkerror)
    >> {
    >> System.err.println("myLibrary was not found.\n\n");
    >> unsatisfiedlinkerror.printStackTrace();
    >> }
    >> }

    >
    > Aren't you loading the same library ("myLibrary") twice then? Once
    > when class X is loaded and once when class Y is loaded. It is up
    > to the programmer to ensure that a native library is only loaded
    > once.
    >
    > Joel


    Sorry, this appears to be false. According to the API docs, subsequent
    calls to loadLibrary should be ignored. However, I remember reading
    in the Tomcat docs that you should ensure that libraries are loaded
    only once...

    Joel
     
    Joel Beach, Nov 13, 2003
    #3
  4. Alex Hunsley

    Alex Hunsley Guest

    Re: IBM JVM: loading two native libraries in JNI, second one hasproblems

    Joel Beach wrote:

    > Joel Beach wrote:
    >
    >
    >>Alex Hunsley wrote:
    >>
    >>
    >>>I've refined my problem using JNI under IBM's JVM1.4.1 (in Linux) to the
    >>>following scenario:
    >>>

    >>
    >><snip>
    >>
    >>>p.s. the native library files (.so files) are loaded due to the
    >>>following static section in classes X and Y:
    >>>
    >>>
    >>> static
    >>> {
    >>> try
    >>> {
    >>> System.loadLibrary("myLibrary");
    >>> }
    >>> catch(UnsatisfiedLinkError unsatisfiedlinkerror)
    >>> {
    >>> System.err.println("myLibrary was not found.\n\n");
    >>> unsatisfiedlinkerror.printStackTrace();
    >>> }
    >>> }

    >>
    >>Aren't you loading the same library ("myLibrary") twice then? Once
    >>when class X is loaded and once when class Y is loaded. It is up
    >>to the programmer to ensure that a native library is only loaded
    >>once.
    >>
    >>Joel

    >
    >
    > Sorry, this appears to be false. According to the API docs, subsequent
    > calls to loadLibrary should be ignored. However, I remember reading
    > in the Tomcat docs that you should ensure that libraries are loaded
    > only once...
    >
    > Joel


    Sorry, I didn't make it clear enough.,.. classes X and Y do load
    *different* libraries, I used the static section above as an example of
    how it was done.
    And as you say, it shouldn't make a difference if you try to reload the
    same library again...

    thanks
    alex
     
    Alex Hunsley, Nov 13, 2003
    #4
  5. Alex Hunsley

    Alex Hunsley Guest

    Re: IBM JVM: loading two native libraries in JNI, second one hasproblems

    Alex Hunsley wrote:


    > Sorry, I didn't make it clear enough.,.. classes X and Y do load
    > *different* libraries, I used the static section above as an example of
    > how it was done.
    > And as you say, it shouldn't make a difference if you try to reload the
    > same library again...
    >
    > thanks
    > alex


    Just to clarify in this thread... the problem has been solved, see my
    other post to this group a few minutes ago in the thread "JNI string
    problem"...
    thanks for the help!
    alex
     
    Alex Hunsley, Nov 14, 2003
    #5
    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. Abhishek Singh

    JVM crashes: Native method, JNI

    Abhishek Singh, Jul 23, 2003, in forum: Java
    Replies:
    2
    Views:
    798
    Nigel Wade
    Jul 23, 2003
  2. Alex Hunsley

    IBM's JNI fails where Sun's JNI works

    Alex Hunsley, Nov 3, 2003, in forum: Java
    Replies:
    4
    Views:
    851
    Alex Hunsley
    Nov 4, 2003
  3. Lasse
    Replies:
    1
    Views:
    700
    Jon A. Cruz
    Jan 5, 2004
  4. Replies:
    4
    Views:
    7,884
    tungjty
    Jun 22, 2011
  5. bgabrhelik
    Replies:
    0
    Views:
    818
    bgabrhelik
    Sep 29, 2009
Loading...

Share This Page