JNI method from static lib in ios?

Bob Vandette bob.vandette at oracle.com
Thu Mar 24 12:32:26 UTC 2016


Here’s an example for libzip.

#ifdef STATIC_BUILD
JNIEXPORT jint JNICALL
JNI_OnLoad_zip(JavaVM *vm, void *reserved)
{
    return JNI_VERSION_1_8;
}
#endif


In JDK9, I added some macros to minimize the changes and ifdefs needed 
throughout the JDK sources.  You could use something like that for your 
application code.

Look for STATIC_BUILD in …

jdk9/jdk/src/java.base/share/native/libjava/jni_util.h


Bob.


> On Mar 24, 2016, at 8:11 AM, Grigory Ptashko <grigory.ptashko at gmail.com> wrote:
> 
> Yes, I’ve found out about JNI static libraries.
> 
> Can you, please, give me the simplest example from JDK9 sources
> that implements JNI_OnLoad_<Lib> ? That would help me a lot..
> 
> Thank you.
> 
>> On 24 марта 2016 г., at 15:00, Bob Vandette <bob.vandette at oracle.com> wrote:
>> 
>> The JNI specification was updated in SE 8 to support static JNI libraries.
>> 
>> The key change is around the JNI_OnLoad function.  
>> 
>> In order for Java to know that a JNI static library is bound to the current executable, 
>> you must have a unique JNI_OnLoad_{library} entry point that includes the name of the library.
>> 
>> Add a JNI_OnLoad_JavaToUIOasteboard entry point to your library when it’s built as a static
>> library.  This will allow the ClassLoader to successfully register it as a JNI library when System.loadLibrary
>> is called.  Take a look at the SE 8 JNI spec and the JDK9 JDK sources for other examples of these static library 
>> entry points.
>> 
>> Bob.
>> 
>> 
>>> On Mar 24, 2016, at 5:50 AM, Grigory Ptashko <grigory.ptashko at gmail.com> wrote:
>>> 
>>> Hello.
>>> 
>>> I'm able to launch jdk9 on ios both on the simulator and device.
>>> And also I've managed to remote debug java running in the simulator.
>>> 
>>> I want to use the JNI method from my java class.
>>> I made a static library libJavaToUIPasteboard.a.
>>> 
>>> I made it a fat library:
>>> 
>>> Here’s what lipo -info libJavaToUIPasteboard.a outputs:
>>> 
>>> Architectures in the fat file: libJavaToUIPasteboard.a are: armv7 i386 x86_64 arm64 
>>> 
>>> 
>>> Here's what nm libJavaToUIPasteboard.a outputs:
>>> 
>>> 0000000000000000 T _Java_EvalJava_stringToUIPasteboard
>>>               U _NSLog
>>> 0000000000009b20 S _OBJC_CLASS_$_JavaToUIPasteboard
>>>               U _OBJC_CLASS_$_NSObject
>>> 0000000000009af8 S _OBJC_METACLASS_$_JavaToUIPasteboard
>>>               U _OBJC_METACLASS_$_NSObject
>>>               U ___CFConstantStringClassReference
>>>               U __objc_empty_cache
>>> 0000000000009ab0 s l_OBJC_CLASS_RO_$_JavaToUIPasteboard
>>> 0000000000009a68 s l_OBJC_METACLASS_RO_$_JavaToUIPasteboard
>>> 
>>> Just copying this file to app wrapper folder and also specifying the
>>> java.library.path gives java.lang.UnsatisfiedLinkError: no JavaToUIPasteboard in java.library.path.
>>> I tried both System.loadLibrary("JavaToUIPasteboard") and System.loadLibrary("libJavaToUIPasteboard").
>>> They both produce UnsatisfiedLinkError.
>>> 
>>> I'm a bit confused. How can I use this static lib from my class via JNI.
>>> 
>>> Thank you.
>>> 
>>> --
>>> Best regards,
>>> Grigory Ptashko
>>> 
>>> +7 (916) 1489766
>>> grigory.ptashko at gmail.com
>>> facebook.com/GrigoryPtashko
>>> 
>> 
> 
> 
> --
> Best regards,
> Grigory Ptashko
> 
> +7 (916) 1489766
> grigory.ptashko at gmail.com
> facebook.com/GrigoryPtashko
> 



More information about the mobile-dev mailing list