tomcat在Linux环境下加载外部so文件问题
项目部署在LINUX环境下的tomcat中 启动后提示
Exception in thread "Thread-2" java.lang.UnsatisfiedLinkError: Unable to load library 'libJavaSense4Pkg': liblibJavaSense4Pkg.so: cannot open shared object file: No such file or directory
at com.sun.jna.NativeLibrary.loadLibrary(NativeLibrary.java:166)
at com.sun.jna.NativeLibrary.getInstance(NativeLibrary.java:239)
at com.sun.jna.Library$Handler.<init>(Library.java:140)
at com.sun.jna.Native.loadLibrary(Native.java:393)
at com.sun.jna.Native.loadLibrary(Native.java:378)
at sunyan.lock.LinuxCheckAdapter.loadLib(LinuxCheckAdapter.java:35)
at sunyan.lock.LinuxCheckAdapter.getLinuxCheckAdapter(LinuxCheckAdapter.java:51)
at sunyan.lock.SystemCheckAdapter.getSystemCheckAdapter(SystemCheckAdapter.java:23)
at sunyan.lock.SysStateCheckThread.run(SysStateCheckThread.java:25)
so文件已经放在了所有可能的目录 包括jre下 usr/lib下等等 仍然提示如上错误,请教各位linux so文件需要特殊处理吗 还是其他地方没写对 我加载lib是用的
System.loadLibrary(name)方法
System.getProperty("java.library.path")得到的路径如下
:/usr/java/jdk1.5.0_21/jre/lib/i386/client:/usr/java/jdk1.5.0_21/jre/lib/i386:/usr/java/jdk1.5.0_21/jre/../lib/i386
在这几个目录下我都放了.so文件
Answers
The important thing to know about using JNI under Tomcat is that one cannot place the native libraries OR their JNI interfaces under the WEB-INF/lib or WEB-INF/classes directories of a web application and expect to be able to reload the webapp without restarting the server. The class that calls System.loadLibrary(String) must be loaded by a classloader that is not affected by reloading the web application itself.
Thus, if you have JNI code that follows the convention of including a static initilaizer like this:
then both this class and the shared library should be placed in the $CATALINA_HOME/shared/lib directory.
Note that under Windows, you'll also need to make sure that the library is in the java.library.path. Either add %CATALINA_HOME%\shared\lib to your Windows PATH environment variable, or place the DLL files in another location that is currently on the java.library.path. There may be a similar requirement for UNIX based system (I haven't checked), in which case you'd also have to add $CATALINA_HOME/shared/lib to the PATH environment variable. (Note: I'm not the original author of this entry.)
The symptom of this problem that I encountered looked something like this -
java.lang.UnsatisfiedLinkError: Native Library WEB-INF/lib/libfoo.so already loaded in another classloader
at java.lang.ClassLoader.loadLibrary0(ClassLoader.java:1525)
If the UnsatisfiedLinkError is intermittent, it may be related to Tomcat's default session manager. It restored previous sessions at startup. One of those objects may load the JNI library. Try stopping the Tomcat JVM, deleting the SESSIONS.ser file, then starting Tomcat. You may consider changing the session persistence manager at this time.
Note that Tomcat 6.0.14 the $CATALINA_HOME/shared/lib directory does not exist. You will need to add this and you will need to edit $CATALINA_HOME/conf/catalina.properties so that the shared.loader line looks like this shared.loader=$CATALINA_HOME/shared/lib