从 Jython 模块使用 JNI 调用本机代码时出错

Error when calling Native Code using JNI from a Jython module

我正在从 Jython 模块(在 PyDev 项目中)调用一个用 C 编写的简单 HelloWorld 程序。有一个中间 Java Class(在单独的 Java 项目中),它使用 JNI(Java 本机接口)调用本机代码。如果我直接运行 Java class,本机调用是成功。但是当我从我的 Jython 模块调用 Java class 时,出现以下错误:

java.lang.UnsatisfiedLinkError: no ctest in java.library.path

我已经使用 JNA(Java Native Access)从我的 Jython 模块中成功 运行 几个 C 程序。但是 JNA 真的很难达到性能(速度),我想重新访问 JNI 并解决这个问题(我也只是想知道发生了什么)。 IDE 我用的是Eclipse。

这是 Java class 的代码:

package JNIPackage;



public class HelloWorld {

  native void helloFromC(); /* (1) */
  static {
     // Added the line below but still no luck. Was sure this would fix it. 
     System.setProperty("java.library.path", "/Users/haiderriaz/Desktop/JNI-C");
     System.loadLibrary("ctest"); /* (2) */
  }
  static public void main(String argv[]) {
     HelloWorld helloWorld = new HelloWorld();
     helloWorld.helloFromC(); /* (3) */
  }

}
直接

运行这个Javaclass,没有错误,打印出来"Hello World"。但是当我将 JNIPackage 导入我的 Jython 模块然后调用 JNIPackage.HelloWorld 时,突然 java 找不到 ctest。我认为这很奇怪,只有在使用 JNI 而不是 JNA 调用 C 代码时才会出现问题。

尝试 System.load([full path and filename of ctest]),它独立于 LD_LIBRARY_PATHjava.library.path.

的值工作

为了简化用户配置,我通常会实现自己的库搜索机制,即让它在工作目录和类路径中查找库。我从意识形态上知道这有点不对,但对您的用户来说工作起来更顺畅。使用 java.io.File.exists 确认 ctest-file 的实际位置,然后使用 java.io.File.getAbsolutePath()System.load 获取适当的输入。 System.mapLibraryName(...) 对此也很有用。