如何通过Java代码影响System.loadLibrary()的搜索路径? [英] How to influence search path of System.loadLibrary() through Java code?

查看:76
本文介绍了如何通过Java代码影响System.loadLibrary()的搜索路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Java项目中,我使用的是第三方库,它通过

In a Java project, I am using a third-party library that loads some native library via

System.loadLibrary("libName");

我希望能够在我的应用程序中影响此方法的搜索路径,所以用户不需要在命令行上指定正确的java.library.path值(此值取决于当前的操作系统和体系结构)。例如在Windows上我想将它设置为lib / native / windows,在Linux 32bit上设置为lib / native / linux32等。

I'd like to be able to influence the search path of this method from within my application, so that the user doesn't need to specify a correct java.library.path value on the command line (this value depends on the current OS and architecture). E.g on Windows I want to set it to "lib/native/windows", on Linux 32bit to "lib/native/linux32" etc.

我试过

System.setProperty("java.library.path", ...)

但这被忽略了,显然是因为JVM在我的代码运行之前只读取了一次这个属性。

but this is ignored, apparently because the JVM reads this property only once before my code is run.

我还尝试在使用依赖它的Java库之前加载本机库

I also tried to load the native libray before using the Java library that depends on it with

System.load("fullPath/lib")

此调用成功,但在再次加载本机库时仍会出现UnsatisfiedLinkError使用System.loadLibrary()。

This call succeeds, but there will still be an UnsatisfiedLinkError when the native library is loaded again with System.loadLibrary().

我找到的唯一方法如下:

The only way I found is the following:


  • 添加抽象外部库的整个API的接口。

  • 在其余代码中仅使用这些接口。

  • 添加类实现接口并委托给库。

  • 写一个自己的ClassLoader,

    • 覆盖findLibary(),以便在正确的路径中找到本机库

    • 覆盖loadClass()并加载所有外部库的类和包装层本身而不是像默认的ClassLoader那样尝试委托给它的父类

    • Add interfaces that abstract the whole API of the external library.
    • Use only these interfaces in the rest of the code.
    • Add classes that implement the interfaces and delegate to the library.
    • Write an own ClassLoader, that
      • overwrites findLibary() so that the native library is found in the correct path
      • overwrites loadClass() and loads all classes of the external library and the wrapper layer by itself instead of trying to delegate to its parent like the default ClassLoader would do

      这有效,但是我发现它非常复杂,而且很费劲,因为我需要添加所有这些接口。是否有更简单的方法?

      This works, but I find it very complicated and it is much effort because I need to add all those interfaces. Is there a simpler way?

      推荐答案


      1. 您无法更改正在运行的JVM的库路径。

      2. 您不能多次加载本机库...并且您无法卸载本机库以便可以再次加载它: http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4171986

      1. You cannot change the library path for a running JVM.
      2. You cannot load a native library more than once ... and you cannot unload a native library so that you can load it again: http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4171986

      根据您上面的评论(特别是第三方库的行为),我会说你最好的选择就是在启动时让库路径正确JVM。

      Base on your comments above (particularly, the behaviour of the 3rd-party library), I'd say that your best option is to get the library path right when you launch the JVM.

      这篇关于如何通过Java代码影响System.loadLibrary()的搜索路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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