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

A

Alex Hunsley

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();
}
}
 
J

Joel Beach

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

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
 
J

Joel Beach

Joel said:
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
 
A

Alex Hunsley

Joel said:
Joel Beach wrote:




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
 
A

Alex Hunsley

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
 

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,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top