Joel Hirsh
2018-11-20 06:03:22 UTC
I am running Tomcat 8.0.32 on LInux, and trying to use a jni library under
tomcat, and am hitting a wall.
First I found that only one instance of the native library can be loaded so
the library needs to live in shared/lib for general use.
Then in order to load the native library I found documentation that I both
needed to load the native library from a bootstrap jar that also lives in
shared/lib and that the jar be loaded with the common clasloader
Ref:
https://wiki.apache.org/tomcat/HowTo#I.27m_encountering_classloader_problems_when_using_JNI_under_Tomcat
So I have my code working to do all that. And if I make a call from the
bootstrap jar to the first native method it works.
But when I try to access the native method from my application code I get
an error "java.lang.UnsatisfiedLinkError:".
I believe that is because the native library has been loaded with the
URLClassLoader and the application is loaded with the WepappClassLoader.
Although I also read that the classloaders are hierarchical, and should go
up the chain to find classes.
I have verified that the URLClassLoader used to load the bootstrap jar is
the same as the parent loader of the application (with .equals())
I did read an ancient thread (2006) that kind of described the problem I am
having, and the last post there indicated that the solution is "to touch
each class that uses native methods (from the loader that loaded the native
library) , forcing the classes to be loaded". But I don't know how to
'touch' a java class. And my classes that use native methods are part of
the application and could not be loaded by the URLClassLoader in any case.
Or even if that post is (still) relevant.
I have run out of ideas on what to do or test next. I've been through doc
like
https://tomcat.apache.org/tomcat-8.0-doc/class-loader-howto.html
and many posts on stackoverflow on this topic, and cannot find anything to
help.
Any suggestions would be most welcome.
tomcat, and am hitting a wall.
First I found that only one instance of the native library can be loaded so
the library needs to live in shared/lib for general use.
Then in order to load the native library I found documentation that I both
needed to load the native library from a bootstrap jar that also lives in
shared/lib and that the jar be loaded with the common clasloader
Ref:
https://wiki.apache.org/tomcat/HowTo#I.27m_encountering_classloader_problems_when_using_JNI_under_Tomcat
So I have my code working to do all that. And if I make a call from the
bootstrap jar to the first native method it works.
But when I try to access the native method from my application code I get
an error "java.lang.UnsatisfiedLinkError:".
I believe that is because the native library has been loaded with the
URLClassLoader and the application is loaded with the WepappClassLoader.
Although I also read that the classloaders are hierarchical, and should go
up the chain to find classes.
I have verified that the URLClassLoader used to load the bootstrap jar is
the same as the parent loader of the application (with .equals())
I did read an ancient thread (2006) that kind of described the problem I am
having, and the last post there indicated that the solution is "to touch
each class that uses native methods (from the loader that loaded the native
library) , forcing the classes to be loaded". But I don't know how to
'touch' a java class. And my classes that use native methods are part of
the application and could not be loaded by the URLClassLoader in any case.
Or even if that post is (still) relevant.
I have run out of ideas on what to do or test next. I've been through doc
like
https://tomcat.apache.org/tomcat-8.0-doc/class-loader-howto.html
and many posts on stackoverflow on this topic, and cannot find anything to
help.
Any suggestions would be most welcome.