如何从 Grails 应用程序使用 JNI 本机库

2024-03-05

我正在开发 Grails Web 应用程序,需要使用 JNI 本机库来访问某些特定硬件。对于一个简单的 Java 应用程序(见下文)它工作得很好。为此,我只需将 JAR 添加到 Java 构建路径并指定“本机库位置”(我在 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");默默地执行,没有任何异常,但下一行我尝试使用我的本机代码Driver driver = Driver.instance(); fails:

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 应用程序中使用本机库的正确方法是什么?


您的 DLL 需要位于 Java 系统属性中指定的路径上java.library.path。在 Windows 上PATH环境变量和Linux的LD_LIBRARY_PATH环境变量被添加到此系统属性中。您可以尝试记录java.library.path系统属性来查看 Java 是否在正确的位置寻找您的 DLL。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何从 Grails 应用程序使用 JNI 本机库 的相关文章

随机推荐