1

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?

Community
  • 1
  • 1
Mihai
  • 600
  • 7
  • 16

1 Answers1

2

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.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • Hello EJP, thank you for your answer, I wasn't focusing on the method signature because as I said, it works fine in a standalone app (i.e. in a main method, load the library and call a native method runs with no problem). I've generated the headers with the javah tool and I implement methods. The native library is compiled correctly. The UnsatisfiedLinkError error shows up only in the tomcat context. – Mihai Jun 19 '13 at 12:00
  • So you are loading an old version of the library in the Tomcat context. – user207421 Jun 19 '13 at 19:05