Unable to load Mac OS X dynamic library .dylib in windows. Exception in thread “main” java.lang.UnsatisfiedLinkError : Can't find dependent libraries

空扰寡人 提交于 2019-12-25 02:50:34

问题


I have created a dynamic library (.dylib) which contains JNI(Java Native Interface) header files and c source files HelloWorld.h and HelloWorld.c using XCode in Mac OS X. JDK version is 1.7.0_51. I have copied "include" folder from "/Library/Java/JavaVirtualMachines/jdk1.7.0.51.jdk/Contents/Home/include" into XCode project. I build the project which created "libJNIToCDynamic.dylib". I followed the blog " http://solutions.weblite.ca/tutorials/jni_osx/" regarding JNI usage.

HelloWorld.h
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class HelloWorld */

#ifndef _Included_HelloWorld
#define _Included_HelloWorld
#ifdef __cplusplus
extern "C" {
#endif
/*
 * Class:     HelloWorld
 * Method:    print
 * Signature: ()V
 */
JNIEXPORT void JNICALL Java_HelloWorld_print
  (JNIEnv *, jobject);

#ifdef __cplusplus
}
#endif
#endif

HelloWorld.c
#include <jni.h>
#include <stdio.h>
#include "HelloWorld.h"

JNIEXPORT void JNICALL
Java_HelloWorld_print(JNIEnv *env, jobject obj)
{
    printf("Hello World! using JNI \n");
    return;
}

I have written a following Java code to load .dylib library using Eclipse in Mac OS X. Here is the code to load library:-

class HelloWorld {

        static {
            System.load("/Users/veerendra/Downloads/libJNIToCDynamic.dylib");
        }

        private native void print();

        public static void main(String[] args) {
            new HelloWorld().print();           
        }
}

Then I generated HelloWorld.class and placed .class and .dylib in one folder. In the terminal I ran HelloWorld.class successfully and prints in the console " Hello World! using JNI". Java runs fine in Mac OS X without errors.

Later, I tried to run java to load same library ".dylib" in Windows using Eclipse tool with path System.load("F:\Android\DependentLibraries\libJNIToCDynamic.dylib") and I got following error:-

Exception in thread "main" java.lang.UnsatisfiedLinkError: F:\Android\DependentLibraries\libJNIToCDynamic.dylib: Can't find dependent libraries at java.lang.ClassLoader$NativeLibrary.load(Native Method) at java.lang.ClassLoader.loadLibrary1(Unknown Source) at java.lang.ClassLoader.loadLibrary0(Unknown Source) at java.lang.ClassLoader.loadLibrary(Unknown Source) at java.lang.Runtime.load0(Unknown Source) at java.lang.System.load(Unknown Source) at HelloWorld.(HelloWorld.java:9)

Dynamic library .dylib generated in Mac OS X loads successfully in Mac but not when tried to load the same .dylib in Windows throws an error.

Dependent libraries by running otool:- Veerendras-MacBook:~ veerendra$ otool -L /Users/veerendra/Downloads/libJNIToCDynamic.dylib /Users/veerendra/Downloads/libJNIToCDynamic.dylib: /usr/local/lib/libJNIToCDynamic.dylib (compatibility version 1.0.0, current version 1.0.0) /System/Library/Frameworks/Foundation.framework/Versions/C/Foundation (compatibility version 300.0.0, current version 945.11.0) /usr/lib/libobjc.A.dylib (compatibility version 1.0.0, current version 228.0.0) /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 169.3.0)

来源:https://stackoverflow.com/questions/21577127/unable-to-load-mac-os-x-dynamic-library-dylib-in-windows-exception-in-thread

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!