如何从Grails应用程序使用JNI本地库 [英] How to use a JNI native library from Grails application

查看:137
本文介绍了如何从Grails应用程序使用JNI本地库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个Grails web应用程序,我需要使用一个JNI本地库来访问某些特定的硬件。对于一个简单的Java应用程序(见下文),它工作正常。为此,我只需将JAR添加到Java构建路径并指定本机库位置(我使用Windows7上的SpringSource工具套件)。



工作示例:

 导入conceptacid.nativedriver.Driver; 
public class Main {
public static void main(String [] args){
System.out.println(starting ...);
System.loadLibrary(AudioCardDriver);
Driver driver = Driver.instance();
字符串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();
字符串driverDescription = driver.getDriverDescription();
System.out.println(Native application driver:+ driverDescription);

def destroy = {
}
}



<第一行 System.loadLibrary(AudioCardDriver); 在没有任何异常的情况下静静执行,但下一行尝试使用我的本机代码 Driver driver = Driver.instance(); 失败:

 运行脚本C:\grails \scripts\RunApp.groovy 
设置为开发的环境
[groovyc]将1个源文件编译为D:\Projects3\mbr\target\classes
[delete]正在删除目录C:\ Users \VShmyrev\.grails\1.3.7\projects\mbr\tomcat
运行Grails应用程序..
2012-02-24 15:19:49,690 [main] ERROR context.GrailsContextLoader - 执行bootstraps时出错:java.lang.UnsatisfiedLinkError:conceptacid.nativedriver.AudioCardDriverJNI.swig_module_init()V
org.codehaus.groovy.runtime.InvokerInvocationException:java.lang.UnsatisfiedLinkError:conceptacid。 nativedriv er.AudioCardDriverJNI.swig_module_init()V
在grails.util.Environment.evaluateEnvironmentSpecificBlock(Environment.java:251)

...

导致: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)
在conceptacid.nativedriver.Driver.instance(Driver.java:35)
在conceptacid.nativedriver.Driver $ instance.call(未知源)
在BootStrap $ _closure1.doCall (BootStrap.groovy:7)
... 26 more
应用程序上下文关闭...

我确信我把DLL放到了我的系统PATH中的一个目录中,但它没有帮助。



什么是正确的方法在开发环境和生产中使用Grails应用程序中的本地库?您的DLL需要位于Java系统属性 java.library.path 中指定的路径上。在Windows上, PATH 环境变量和Linux LD_LIBRARY_PATH 环境变量被添加到此系统属性中。您可以尝试记录 java.library.path 系统属性以查看Java是否在您的DLL的正确位置。


I'm developing a Grails web application and I need to use a JNI native library to access some specific hardware. For a simple Java application (see below) it works fine. To do this I just have to add the JAR to the Java build path and specify the "Native library location" (I use SpringSource Tool Suite on Windows7).

Working example:

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);
    }
}

However, when I try to add it into my Grails application it fails: 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 = {
    }
}

the first line System.loadLibrary("AudioCardDriver"); is executed silently without any exception but the next line where I try to use my native code 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...

I'm sure I put the DLL into a directory which is in my system PATH but it doesn't help.

What is the right way to use a native library in Grails application both in a development environment and in production?

解决方案

Your DLL needs to be on a path specified in the Java system property java.library.path. On Windows the PATH environment variable and Linux the LD_LIBRARY_PATH environment variable are added to this system property. You can try logging the java.library.path system property to see if Java is looking in the right place for your DLL.

这篇关于如何从Grails应用程序使用JNI本地库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆