可移植语句使用相对路径名从不同目录加载JNI库? [英] portable statement to load JNI library from a different directory using relative pathname?

查看:68
本文介绍了可移植语句使用相对路径名从不同目录加载JNI库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否存在与平台无关的Java语句,用于从与Java源代码不同的目录中加载本地库?我想使用这样的东西:

Is there a platform-independent Java statement to load a native library from a different directory than the Java source code is in? I would like to use something like this:

public class HelloWorld {
    static {
        System.loadLibrary("../some_project/HelloWorld");
    }

    public static native void print();
}

问题在于System.loadLibrary()在pathname参数中不支持目录分隔符.另外,不幸的是,System.load()需要一个绝对路径名,这不仅意味着我不能如上所述指定一个相对目录(我想这样做),而且还要求该参数包括例如前面的内容.在Linux系统上,JNI库名称的扩展名为"lib"和".so".

The problem is that System.loadLibrary() doesn't support directory separators in the pathname argument. Also, System.load() unfortunately requires an absolute pathname, which not only means I can't specify a relative directory as above (which I would like to do), but it also requires the argument to include, for example, the preceding "lib" and ".so" extension on the JNI library name on a Linux system.

是否有处理此问题的标准方法?如果可能的话,我想避免编写一堆依赖平台的Java代码,只是为了构造正确的JNI库名称.

Is there a standard way of dealing with this? If possible, I would like to avoid writing a bunch of platform-dependent Java code just to construct the correct JNI library name.

推荐答案

我相信您正在寻找System.mapLibraryName ,这是

I believe you're looking for System.mapLibraryName, which is the method typically used by ClassLoader.findLibrary implementations. For example:

File lib = new File("../some_project/" + System.mapLibraryName("HelloWorld"));
System.load(lib.getAbsolutePath());

这将在Linux上使用libHelloWorld.so,在Windows上使用HelloWorld.dll.请注意,某些操作系统设计支持多种扩展,而mapLibraryName仅支持一种扩展.我知道的是MacOS(主要是.dylib.jnilib)和AIX(.a.so).

This will use libHelloWorld.so on Linux and HelloWorld.dll on Windows. Be aware that some operating systems support multiple extensions, and mapLibraryName can only support one, by design. The ones I'm aware of are MacOS (.dylib primarily and .jnilib for legacy) and AIX (.a and .so).

这篇关于可移植语句使用相对路径名从不同目录加载JNI库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆