Java-tomcat在Linux环境下加载外部so文件问题

UI设计界面 UI设计界面 主题:1059 回复:2190

Java-tomcat在Linux环境下加载外部so文件问题

夜无邪 发布于 2017-08-14 字数 1357 浏览 1523 回复 3

项目部署在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文件

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

支持 Markdown 语法,需要帮助?

评论(3

偏爱自由 2017-09-16 3 楼

1.System.loadLibrary 参数为库文件名,不包含库文件的扩展名
必须是在java.library.path这一jvm变量所指向的路径中
可以通过如下方法来获得该变量的值:
System.getProperty("java.library.path");

2.可以试试 System.load 参数为库文件的绝对路径。

瑾兮 2017-09-13 2 楼

不满足链接错,需要把native和其依赖的所有so文件放到环境变量LD_LIBRARY_PATH定义的目录下。文件依赖关系可以用ldd命令查看。

晚风撩人 2017-08-19 1 楼

你可以看看:
http://wiki.apache.org/tomcat/HowTo#I.27m_encountering_classloader_problems_when_using_JNI_under_Tomcat

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:

class FooWrapper {
static {
System.loadLibrary("foo");
}

native void doFoo();

}

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%sharedlib 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