macos 在 Mac OS X 上加载 JNI 库?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2550571/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Loading JNI lib on Mac OS X?
提问by Clinton
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:
所以我试图在运行时将 jnilib(特别是JOGL)加载到 Mac OS X 上的 Java 中。我一直在关注相关的 Stack Overflow 问题:
- Maven and the JOGL Library
- Loading DLL in Java - Eclipse - JNI
- How to make a jar file that include all jar files
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:
我的最终目标是将特定于平台的 JOGL 文件打包到 JAR 中并将它们解压缩到临时目录中并在启动时加载它们。我解决了我的问题,只是尝试使用硬编码路径加载 JOGL:
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:
尝试使用 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:
但是当我java.library.path
通过添加以下 JVM 选项来指定时:
-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?
是否可以System.load
在 Mac OS X 上使用(或其他一些变体)作为在运行时调用的 -Djava.library.path 的替代品?
采纳答案by momania
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()
Jogl 总是尝试自动加载所有依赖库。为了避免这种情况,应该有一个 NativeLibLoader 类,您可以在通过 System.load() 自己加载库之前调用 disableLoading()
回答by David Sauter
You don't have to provide the java.library.path
at startup. You can programmatically set it with
您不必java.library.path
在启动时提供。您可以以编程方式设置它
System.setProperty("java.library.path", "/var/folder/bla/foo/bar/");
I don't know if System.load()
will work somehow without this library path.
我不知道如果System.load()
没有这个库路径是否会以某种方式工作。
回答by Santhosh Kumar Tekuri
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;
System.load(...)
将 libraryName 作为参数。它不以库路径作为参数。JVM 在指定的路径列表中搜索具有指定名称的库-Djava.library.path;
Here there is nothing specific to Mac OS X. It searches for libraries in the same way on all operating systems.
这里没有任何特定于 Mac OS X 的内容。它在所有操作系统上以相同的方式搜索库。