Loading JNI lib on Mac OS X?

2019-04-28 05:29发布

Background

So I am attempting to load a jnilib (specifically JOGL) into Java on Mac OS X at runtime. I have been following along the relevant Stack Overflow questions:

The end goal for me is to package platform specific JOGL files into a JAR and unzip them into a temp directory and load them at start-up. I worked my problem back to simply attempting to load JOGL using hard-coded paths:

    File f = new File("/var/folders/+n/+nfb8NHsHiSpEh6AHMCyvE+++TI/-Tmp-/libjogl.jnilib");
    System.load(f.toString());
    f = new File ("/var/folders/+n/+nfb8NHsHiSpEh6AHMCyvE+++TI/-Tmp-/libjogl_awt.jnilib");
    System.load(f.toString());

I get the following exception when attempting to use the JOGL API:

    Exception in thread "main" java.lang.UnsatisfiedLinkError: no jogl in java.library.path

But when I specify java.library.path by adding the following JVM option:

    -Djava.library.path="/var/folders/+n/+nfb8NHsHiSpEh6AHMCyvE+++TI/-Tmp-/" 

Everything works fine.


Question

Is it possible use System.load (or some other variant) on Mac OS X as a replacement for -Djava.library.path that is invoked at runtime?

3条回答
该账号已被封号
2楼-- · 2019-04-28 05:55

System.load(...) takes libraryName as argument. It doesn't take path to library as argument. JVM searches for a library with specified name in the list of paths specified in -Djava.library.path;

Here there is nothing specific to Mac OS X. It searches for libraries in the same way on all operating systems.

查看更多
聊天终结者
3楼-- · 2019-04-28 05:58

Jogl always tries to auto-load all dependent libraries. To avoid this, there should be a NativeLibLoader class where you can call disableLoading() before you load the libraries yourself via the System.load()

查看更多
我欲成王,谁敢阻挡
4楼-- · 2019-04-28 06:09

You don't have to provide the java.library.path at startup. You can programmatically set it with

System.setProperty("java.library.path", "/var/folder/bla/foo/bar/");

I don't know if System.load() will work somehow without this library path.

查看更多
登录 后发表回答