JNI

Michael Hall mik3hall at gmail.com
Tue Feb 14 02:53:26 PST 2012


On Feb 14, 2012, at 2:45 AM, Michael McMahon wrote:

> On 14/02/12 00:26, Michael Hall wrote:
>> On Feb 13, 2012, at 5:55 PM, Michael Hall wrote:
>> 
>>> On Feb 13, 2012, at 4:34 PM, Scott Kovatch wrote:
>>> 
>>>> What's the name of your JNI library? If it ends in .jnilib it won't work in 1.7. It needs to be .dylib.
>>>> 
>>>> Why yes, that did break your application. See 7134701.
>> OK, simple enough I guess. 1.6 loads if the native lib is just in classpath. 1.7 needs the explicit -Djava.library.path
>> 
>>  /usr/libexec/java_home -v 1.7 --exec java -Djava.library.path=/Users/mjh/HalfPipe -cp .:halfpipe.jar TestMonitor
>> TestMonitor: terminated Adobe Reader /Applications/Adobe Reader.app pid = 554
>> 
>> Acceptable behavior?
>> 
> Loading native libraries from the classpath sounds very strange to me. Can anyone shed some light
> on the reason why that was done?
> 
> - Michael.
> 


> When the Java virtual machine starts up, it constructs a list of directories that will be used to locate native libraries for application classes. The contents of the list are dependent upon the host environment and the virtual machine implementation. For example, under the Win32 JDK or Java 2 SDK releases, the list of directories consists of the Windows system directories, the current working directory, and the entries in the PATH environment variable. Under the Solaris JDK or Java 2 SDK releases, the list of directories consists of the entries in the LD_LIBRARY_PATHenvironment variable.

Overview of the JNI Design
http://java.sun.com/docs/books/jni/html/design.html
11.2.3 Locating Native Libraries

Although _I thouht_ I remembered classpath would work actually 1.6 seems to be working because the lib was in the current directory.
Mac-User:HalfPipe mjh$ mv libhp.dylib ~/Desktop/libhp.dylib
Mac-User:HalfPipe mjh$ cd ~/Desktop
Mac-User:Desktop mjh$ /usr/libexec/java_home -v 1.6 --exec java -cp /Users/mjh/HalfPipe:/Users/mjh/HalfPipe/halfpipe.jar TestMonitor
TestMonitor: terminated Adobe Reader /Applications/Adobe Reader.app pid = 207

Mac-User:Desktop mjh$ mv libhp.dylib ~/HalfPipe/libhp.dylib
Mac-User:Desktop mjh$ /usr/libexec/java_home -v 1.6 --exec java -cp /Users/mjh/HalfPipe:/Users/mjh/HalfPipe/halfpipe.jar TestMonitor
Exception in thread "main" java.lang.UnsatisfiedLinkError: no hp in java.library.path

So the 1.7 behavior difference is in not searching current working directory.

Is this behavior difference acceptable or should there be a bug report?

It might be expected behavior by some existing OS X java applications. Although it seems 'dependent upon the host environment and the virtual machine implementation' so it could be changeable you might run into existing applications that depend on the current behavior. It sounds like including current working directory would also make it sort of cross-platform behavior with Windows. LD_LIBRARY_PATH doesn't seem to be default set for me so Solaris compatibility doesn't appear to be an option.



More information about the macosx-port-dev mailing list