使用 Grails 应用程序中的 JNI 本机库时出现 UnsatisfiedLinkError [英] UnsatisfiedLinkError when using a JNI native library from Grails application

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

问题描述

我有一个需要使用本机库的应用程序:libfoo.so

I have an application where I need to use a Native Library: libfoo.so

我的代码如下:

Accessor.java:

public class Accessor {        
    static {
        String path = "/usr/lib/libfoo.so";
        System.load(path);
    }
    ...
}

当我在独立的 tomcat 服务器中部署我的 war 文件时,这工作得很好.

This works perfectly fine when I deploy my war file in a standalone tomcat server.

问题是当我尝试运行嵌入式tomcat服务器时,您运行:

The problem is when I try to run the embedded tomcat server when you run:

grails run-app

我收到一个 UnsatisfiedLinkError:

I get an UnsatisfiedLinkError:

Caused by UnsatisfiedLinkError: com.foo.bar.GFS_MALJNI.new_Accessor__SWIG_0(Ljava/lang/String;I)J
->>   39 | <init>    in com.foo.bar.Accessor 

有趣的是,如果我将 BuildConfig.groovy 文件更改为 fork 模式,它也可以工作.

Interestingly enough, if I change my BuildConfig.groovy file to fork mode, it also works.

BuildConfig.groovy:

grails.project.fork = [
   run: [maxMemory:1024, minMemory:64, debug:false, maxPerm:256]
]

我不想在 fork 模式下运行它.

I do not want to run it in fork mode.

推荐答案

我注意到正在使用两种不同的类加载器.

I noticed that two different class loaders are being used.

在非分叉模式下,使用了这个类加载器:java.net.URLClassLoader

In the non-forked mode, this class loader was being used: java.net.URLClassLoader

在分叉模式下,使用了这个类加载器:groovy.lang.GroovyClassLoader

In the forked mode, this class loader was being used: groovy.lang.GroovyClassLoader

本机库在分叉模式下正常工作,所以我需要想出一个技巧,在非分叉模式下使用 GroovyClassLoader 加载库.

The native library works correctly in the forked mode, so I needed to come up with a hack to load the library with the GroovyClassLoader in the non-forked mode.

JDK 源中 System.load 是这样定义的:

This is how System.load is defined in the JDK Source:

System.java:

public final class System {
    ...
    public static void load(String filename) {
        Runtime.getRuntime().load0(getCallerClass(), filename);
    }
    ...
}

它使用类加载器和文件名调用 load0.显而易见的解决方案是使用您自己的类加载器调用 load0,但您不能调用它,因为它是包保护的.

It's calling load0 with the classloader and filename. The obvious solution is to call load0 with your own classloader, but you can't call it since it is package-protected.

当您在 groovy 中编写代码时,您可以访问受包保护的私有方法/变量.

When you write code in groovy, you have access to packge-protected and private methods/variables.

我可以指定我自己的类加载器并加载库,如下所示:

I can specify my own classloader and load the library, as such:

class Accessor {        
    static {
        String path = "/usr/lib/libfoo.so"
        //System.load(path);
        Runtime.getRuntime().load0(groovy.lang.GroovyClassLoader.class, path)
    }
    ...
}

我刚刚试过了,它在非分叉模式下工作.

I just tried it, and it's working in non-forked mode.

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

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