在 Mac OS X 上加载 JNI 库?

2022-01-25 00:00:00 macos java java-native-interface jogl

背景

所以我试图在 Mac OS 上将 jnilib(特别是 JOGL)加载到 Java 中X 在运行时.我一直在关注相关的 Stack Overflow 问题:

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:

  • Maven 和 JOGL 库
  • 在 Java - Eclipse - JNI 中加载 DLL
  • 如何制作一个包含所有jar文件的jar文件

我的最终目标是将特定于平台的 JOGL 文件打包到 JAR 中,然后将它们解压缩到临时目录并在启动时加载它们.我解决了我的问题,简单地尝试使用硬编码路径加载 JOGL:

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());

尝试使用 JOGL API 时出现以下异常:

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

但是当我通过添加以下 JVM 选项来指定 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-/" 

一切正常.

问题

是否可以在 Mac OS X 上使用 System.load(或其他一些变体)来替代在运行时调用的 -Djava.library.path?

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?

推荐答案

Jogl 总是尝试自动加载所有依赖库.为了避免这种情况,应该有一个 NativeLibLoader 类,您可以在通过 System.load() 自己加载库之前调用 disableLoading()

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()

相关文章