Shared native library in Tomcat UnsatisfiedLinkError

旧巷老猫 提交于 2019-12-11 09:23:46

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/17137510/shared-native-library-in-tomcat-unsatisfiedlinkerror

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!