我不认为这个问题与 Rhino 有特别的关系。相反,它是 Linux 和 Windows 系统库之间的差异。
JNA 的入门文档(位于
https://github.com/twall/jna/blob/master/www/GettingStarted.md )提供了以下示例:
package com.sun.jna.examples;
import com.sun.jna.Library;
import com.sun.jna.Native;
import com.sun.jna.Platform;
/** Simple example of JNA interface mapping and usage. */
public class HelloWorld {
// This is the standard, stable way of mapping, which supports extensive
// customization and mapping of Java to native types.
public interface CLibrary extends Library {
CLibrary INSTANCE = (CLibrary)
Native.loadLibrary((Platform.isWindows() ? "msvcrt" : "c"),
CLibrary.class);
void printf(String format, Object... args);
}
public static void main(String[] args) {
CLibrary.INSTANCE.printf("Hello, World\n");
for (int i=0;i < args.length;i++) {
CLibrary.INSTANCE.printf("Argument %d: %s\n", i, args[i]);
}
}
}
加载的本机库取决于您运行程序的平台:对于 Windows,您需要使用“msvcrt”(Microsoft Visual C 运行时)库;对于其他人,请使用“c”库。
我运行了一个测试用例来确认:
如果我尝试加载“c”库,则在 Windows 上运行会失败。
PS C:\Users\jharig\workspace\JNATest> java -cp ".\jna-3.5.2.jar;.\platform-3.5.2.jar;.\js.jar;.\js-14.jar" org.mozilla.javascript.tools.shell.Main
Rhino 1.7 release 4 2012 06 18
js> var c = Packages.com.sun.jna.NativeLibrary.getInstance("c");
Exception in thread "main" java.lang.UnsatisfiedLinkError: Unable to load library 'c': JNA native support (win32-amd64/c
.dll) not found in resource path (.\jna-3.5.2.jar;.\platform-3.5.2.jar;.\js.jar;.\js-14.jar)
at com.sun.jna.NativeLibrary.loadLibrary(NativeLibrary.java:220)
at com.sun.jna.NativeLibrary.getInstance(NativeLibrary.java:322)
如果我使用“msvcrt”库,则在 Windows 上运行会成功。
PS C:\Users\jharig\workspace\JNATest> java -cp ".\jna-3.5.2.jar;.\platform-3.5.2.jar;.\js.jar;.\js-14.jar" org.mozilla.javascript.tools.shell.Main
Rhino 1.7 release 4 2012 06 18
js> var c = Packages.com.sun.jna.NativeLibrary.getInstance("msvcrt");
js> var f = c.getFunction("printf");
js> f.invoke(["Hello World\n"]);
Hello World
js>