Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Java (http://www.velocityreviews.com/forums/f30-java.html)
-   -   IBM JVM: loading two native libraries in JNI, second one has problems (http://www.velocityreviews.com/forums/t128644-ibm-jvm-loading-two-native-libraries-in-jni-second-one-has-problems.html)

Alex Hunsley 11-13-2003 12:21 PM

IBM JVM: loading two native libraries in JNI, second one has problems
 
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();
}
}





Joel Beach 11-13-2003 01:30 PM

Re: IBM JVM: loading two native libraries in JNI, second one has problems
 
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 11-13-2003 01:34 PM

Re: IBM JVM: loading two native libraries in JNI, second one has problems
 
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

Alex Hunsley 11-13-2003 03:01 PM

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 11-14-2003 10:08 AM

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



All times are GMT. The time now is 07:19 AM.

Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.