我正在开发 Grails Web 应用程序,我需要使用 JNI native 库来访问某些特定硬件。对于一个简单的 Java 应用程序(见下文)它工作得很好。为此,我只需将 JAR 添加到 Java 构建路径并指定“ native 库位置”(我在 Windows7 上使用 SpringSource Tool Suite)。
工作示例:
import conceptacid.nativedriver.Driver;
public class Main {
public static void main(String[] args) {
System.out.println("starting...");
System.loadLibrary("AudioCardDriver");
Driver driver = Driver.instance();
String driverDescription = driver.getDriverDescription();
System.out.println( "Native application driver: " + driverDescription);
}
}
但是,当我尝试将其添加到我的 Grails 应用程序中时,它失败了: Bootstrap.groovy:
import conceptacid.nativedriver.Driver;
class BootStrap {
def init = { servletContext ->
System.loadLibrary("AudioCardDriver");
Driver driver = Driver.instance();
String driverDescription = driver.getDriverDescription();
System.out.println( "Native application driver: " + driverDescription);
}
def destroy = {
}
}
第一行 System.loadLibrary("AudioCardDriver");
默默执行,没有任何异常,但下一行我尝试使用我的 native 代码 Driver driver = Driver.instance( );
失败:
Running script C:\grails\scripts\RunApp.groovy
Environment set to development
[groovyc] Compiling 1 source file to D:\Projects3\mbr\target\classes
[delete] Deleting directory C:\Users\VShmyrev\.grails\1.3.7\projects\mbr\tomcat
Running Grails application..
2012-02-24 15:19:49,690 [main] ERROR context.GrailsContextLoader - Error executing bootstraps: java.lang.UnsatisfiedLinkError: conceptacid.nativedriver.AudioCardDriverJNI.swig_module_init()V
org.codehaus.groovy.runtime.InvokerInvocationException: java.lang.UnsatisfiedLinkError: conceptacid.nativedriver.AudioCardDriverJNI.swig_module_init()V
at grails.util.Environment.evaluateEnvironmentSpecificBlock(Environment.java:251)
...
Caused by: java.lang.UnsatisfiedLinkError: conceptacid.nativedriver.AudioCardDriverJNI.swig_module_init()V
at conceptacid.nativedriver.AudioCardDriverJNI.swig_module_init(Native Method)
at conceptacid.nativedriver.AudioCardDriverJNI.<clinit>(AudioCardDriverJNI.java:70)
at conceptacid.nativedriver.Driver.instance(Driver.java:35)
at conceptacid.nativedriver.Driver$instance.call(Unknown Source)
at BootStrap$_closure1.doCall(BootStrap.groovy:7)
... 26 more
Application context shutting down...
我确信我已将 DLL 放入系统 PATH 中的目录中,但这没有帮助。
在开发环境和生产环境中,在 Grails 应用程序中使用 native 库的正确方法是什么?
最佳答案
您的 DLL 需要位于 Java 系统属性 java.library.path
中指定的路径上。在 Windows 上,PATH
环境变量和 Linux 上的 LD_LIBRARY_PATH
环境变量将添加到此系统属性中。您可以尝试记录 java.library.path
系统属性,以查看 Java 是否在正确的位置查找您的 DLL。
关于java - 如何从 Grails 应用程序使用 JNI native 库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9432460/