如何通过Java中的代码加载第三方库(如X11库)?

时间:2023-01-18 00:26:53

Whenever JVM fires up, i.e when java command is run; it looks for other libraries in /java/jre/lib folder. These libraries along with the third party libraries like X11 libs, are loaded into the memory by the system's dynamic loader (like dld.so in HP Unix).

每当JVM启动时,即运行java命令时;它在/ java / jre / lib文件夹中查找其他库。这些库以及第三方库(如X11库)由系统的动态加载程序(如HP Unix中的dld.so)加载到内存中。

So is it possible to load third party libraries from code in java? If yes, what could be the side effects?

那么可以从java中的代码加载第三方库吗?如果是的话,副作用可能是什么?

2 个解决方案

#1


What you are looking for is the Java Native Interface (JNI). Using external native libraries will make your code less portable. It will make your code less stable since none of the Java language guarantees will hold. There can be security manager implications since you application will need permissions to load the library. In my experience there are many difficulties in writing good JNI code; specifically deallocation and debugging.

您正在寻找的是Java Native Interface(JNI)。使用外部本机库会降低代码的可移植性。它将使您的代码不那么稳定,因为Java语言保证都不会成立。由于您的应用程序需要加载库的权限,因此可能存在安全管理器的影响。根据我的经验,编写好的JNI代码有很多困难;特别是解除分配和调试。

SWIG may be of use automatically generate the necessary JNI code.

SWIG可能会自动生成必要的JNI代码。

#2


Native libraries are loaded with System/Runtime.loadLibrary. You will then need some code to make translate between the libraries native calls and JNI (Java Native Interface). Then you will need some Java code with native methods defined for calling into the translation code.

本机库使用System / Runtime.loadLibrary加载。然后,您将需要一些代码来在库本机调用和JNI(Java本机接口)之间进行转换。然后,您将需要一些Java代码,其中定义了用于调用转换代码的本机方法。

#1


What you are looking for is the Java Native Interface (JNI). Using external native libraries will make your code less portable. It will make your code less stable since none of the Java language guarantees will hold. There can be security manager implications since you application will need permissions to load the library. In my experience there are many difficulties in writing good JNI code; specifically deallocation and debugging.

您正在寻找的是Java Native Interface(JNI)。使用外部本机库会降低代码的可移植性。它将使您的代码不那么稳定,因为Java语言保证都不会成立。由于您的应用程序需要加载库的权限,因此可能存在安全管理器的影响。根据我的经验,编写好的JNI代码有很多困难;特别是解除分配和调试。

SWIG may be of use automatically generate the necessary JNI code.

SWIG可能会自动生成必要的JNI代码。

#2


Native libraries are loaded with System/Runtime.loadLibrary. You will then need some code to make translate between the libraries native calls and JNI (Java Native Interface). Then you will need some Java code with native methods defined for calling into the translation code.

本机库使用System / Runtime.loadLibrary加载。然后,您将需要一些代码来在库本机调用和JNI(Java本机接口)之间进行转换。然后,您将需要一些Java代码,其中定义了用于调用转换代码的本机方法。