Tomcat中的共享本机库UnsatisfiedLinkError [英] Shared native library in Tomcat UnsatisfiedLinkError

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

问题描述

我需要在Tomcat中加载本机库,供Web应用程序使用。我已经创建了一个包装类,该包装类调用System.load( path / to / library),如下所述:
http://wiki.apache.org/tomcat/HowTo#I.27m_encountering_classloader_problems_when_using_JNI_under_Tomcat



是类似于链接中的链接:

 公共类FooWrapper {
public native void doFoo();
}

我能够从独立的应用程序调用doFoo()(这意味着用C编写的本地方法Java_packagename_FooWrapper_doFoo(...)已正确导出)。
但是,当我从Web应用程序调用doFoo方法时,会得到:

  java.lang.UnsatisfiedLinkError:packagename .FooWrapper.doFoo()V 

我能够获得由加载的本机库的列表ClassLoader使用此处描述的技巧:如何我可以获得加载的JNI库的列表吗?

  java.lang.reflect.Field loadLibraries = ClassLoader .class.getDeclaredField( loadedLibraryNames); 
loadingLibraries.setAccessible(true);
final Vector< String> librarys =(Vector< String>)loadedLibraries.get(ClassLoader.getSystemClassLoader());

我的本​​机库在库矢量中列出,因此调用System.load( ...)被执行而没有任何异常。但是,当我从Web应用程序调用doFoo()时,似乎Java无法在本机库中找到合适的函数。我想念什么?

解决方案

您丢失了问题不是正在加载库,而这正是您大多数问题的答案:是方法签名。它与加载的库中的实际内容不符。自生成.h文件以来,您是否更改过Java本机方法签名?如果是这样,请重新生成它。 .h文件与.c或.cpp文件是否一致?您是否在.c / .cpp文件中包括.h文件?所有这些条件都是必需的。


I need to load a native library in Tomcat which will be used by web apps. I've created a wrapper class that calls System.load("path/to/library") just as described in: http://wiki.apache.org/tomcat/HowTo#I.27m_encountering_classloader_problems_when_using_JNI_under_Tomcat

My class definition is similar to the one in the link:

public class FooWrapper {
    public native void doFoo();
}

I am able to call doFoo() from a standalone application (which means that the native method Java_packagename_FooWrapper_doFoo(...) written in C is exported correctly). However, when I call the doFoo method from a web app I get:

java.lang.UnsatisfiedLinkError: packagename.FooWrapper.doFoo()V

I am able to get a list of the native libraries loaded by the ClassLoader using the trick described here: How do I get a list of JNI libraries which are loaded?

   java.lang.reflect.Field loadedLibraries = ClassLoader.class.getDeclaredField("loadedLibraryNames");
   loadedLibraries.setAccessible(true);
   final Vector<String> libraries = (Vector<String>) loadedLibraries.get(ClassLoader.getSystemClassLoader());

and my native library is listed in the libraries vector, therefore the static block that calls System.load(...) is executed without any exception. However, it seems that Java cannot find a suitable function in the native library when I call doFoo() from a web app. What am I missing?

解决方案

You are missing that the problem isn't loading the library, which is what most of your question is devoted to: it is the method signature. It doesn't agree with whatever is actually in the library that was loaded. Have you changed the Java native method signature since you generated your .h files? If so, regenerate it. Does your .h file agree with your .c or .cpp file? Do you include the .h file in the .c/.cpp file? All of these conditions are required.

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

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