Tomcat中的共享JNI库(.so)-UnsatisfiedLinkError [英] Shared JNI library (.so) in Tomcat - UnsatisfiedLinkError

查看:155
本文介绍了Tomcat中的共享JNI库(.so)-UnsatisfiedLinkError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个在Tomcat7中部署的两个Web应用程序之间共享的JNI库(.so).我在部署的第一个Web应用程序中仅使用System.loadLibrary加载了一次库,然后在第二个Web应用程序中,我正在检查它是否已经加载而不再加载(我尝试同时加载它和得到UnsatisfiedLinkError -库是由另一个类加载器加载的.我可以在第一个应用程序中对本机库进行任何调用,但是在第二个应用程序中,我会收到UnsatisfiedLinkError及其尝试调用的方法名.

I have a JNI library (.so) shared between two web applications deployed in Tomcat7. I am loading the library using the System.loadLibrary only once in the first web application that is being deployed and then in the second I'm checking if it already was loaded to not load anymore (I tried loading it in both and I got UnsatisfiedLinkError - library was loaded by another classloader). I can make any call to the native library in the first application, but in the second one I get UnsatisfiedLinkError with the method name that I am trying to call.

我已经无能为力了.有任何想法吗?我在SO上尝试了大多数解决方案. 谢谢.

I am running out of ideas of what I can do. Any ideas? I tried most of the solutions on SO. Thank you.

编辑 是的,我尝试将库添加到tomcat lib文件夹中并从那里加载它.最初它位于bin文件夹中,并且发生相同的问题.

EDIT Yes, I tried adding the library in the tomcat lib folder and loading it from there. Initially it was in the bin folder and the same issue occurs.

推荐答案

是的,当您尝试加载已经加载了另一个Web应用程序的库时,就会发生这种情况. Tomcat为每个Web应用程序使用单独的类加载器,并且它不允许您通过另一个类加载器多次将同一个本机库加载到JVM

Yes, this will happen when you try to load the library that has already loaded my another web application. Tomcat, uses separate class loaders for each of the web application, and it wont allow you load a same native library more than once to JVM via another class loader

如果任何共享jar文件从您的sharedlib.so中消耗了JNI,则将其移动.将系统路径添加到sharedlib,

Move any share jar files if any that consumes JNI from you sharedlib.so. Add the system path to sharedlib ,

export LD_LIBRARY_PATH=/path/to/whereyourlinklibrary

编写一个像这样的简单类,使您可以在tomcat启动时加载共享库.只需编译此类并将其放入tomcat lib文件夹

Write a simple class like this which enables you to load your shared library when tomcat starts. Just compile this class and drop it in tomcat lib folder

package msm;
public class DLLBootstrapper {

     static {
      System.loadLibrary("sharedlib");
     }

     public static void main(String args[]) {
      System.out.println("Loaded");
     }

    }

您现在可以从任何Web应用程序中加载此类(可能在启动侦听器中)

you can now load this class from any of your web application ( probably in startup listener)

Class.forName("msm.DLLBootstrapper");

很好!

这篇关于Tomcat中的共享JNI库(.so)-UnsatisfiedLinkError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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