从 tomcat 中的 .so 调用 Java 中的本机库时出错
我编写了一个独立的 Java 程序(可以运行),它通过生成 libipmi_agent.so lib 调用从 C 程序创建的本机库,但在 tomcat 中的 Web 应用程序中运行它会给出以下内容错误:
native library call java.lang.reflect.InvocationTargetException
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
...
java.lang.UnsatisfiedLinkError: org.qcri.power.util.IPMIAgent.ipmi_agent_init()I
org.qcri.power.util.IPMIAgent.ipmi_agent_init(Native Method)
org.qcri.power.util.IPMIAgent.main(IPMIAgent.java:18)
...
这是我的 Java 类:
package org.qcri.power.util;
public class IPMIAgent
{
private native int ipmi_agent_init();
private native void ipmi_agent_close();
private native int ipmi_agent_read_current_value();
static
{
System.loadLibrary("ipmi_agent");
}
// The main program
public static int main(String[] args)
{
int i, v=0;
IPMIAgent ipmiagent = new IPMIAgent();
ipmiagent.ipmi_agent_init();
for (i = 0; i < 100; i++)
{
try{
v = ipmiagent.ipmi_agent_read_current_value();
System.out.println("Current value is " + v);
Thread.currentThread().sleep(1000);
}
catch(InterruptedException ie){
}
}
return v;
}
}
libipmi_agent.so 与 /webapps/myapp/WEB_INF/classes 下的上述 Java 类位于同一类文件夹中。
文件的位置是否正确?有人有主意吗?
提前致谢。
I wrote a stand alone Java program (THAT WORKS) it calls a native library created from a C program by generating the libipmi_agent.so lib, but running it in a web-app in tomcat is giving the following error:
native library call java.lang.reflect.InvocationTargetException
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
...
java.lang.UnsatisfiedLinkError: org.qcri.power.util.IPMIAgent.ipmi_agent_init()I
org.qcri.power.util.IPMIAgent.ipmi_agent_init(Native Method)
org.qcri.power.util.IPMIAgent.main(IPMIAgent.java:18)
...
Here is my Java class:
package org.qcri.power.util;
public class IPMIAgent
{
private native int ipmi_agent_init();
private native void ipmi_agent_close();
private native int ipmi_agent_read_current_value();
static
{
System.loadLibrary("ipmi_agent");
}
// The main program
public static int main(String[] args)
{
int i, v=0;
IPMIAgent ipmiagent = new IPMIAgent();
ipmiagent.ipmi_agent_init();
for (i = 0; i < 100; i++)
{
try{
v = ipmiagent.ipmi_agent_read_current_value();
System.out.println("Current value is " + v);
Thread.currentThread().sleep(1000);
}
catch(InterruptedException ie){
}
}
return v;
}
}
the libipmi_agent.so is in the same class folder with the the above Java class under /webapps/myapp/WEB_INF/classes.
is the position of the file correct? anyone has an idea?
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
该错误告诉您它找不到该库,所以不,该位置不正确。
http://wiki.apache.org/tomcat/HowTo#I.27m_encountering_classloader_problems_when_using_JNI_under_Tomcat
(FAQ中的错误不同,但问题是相同的,那里的解决方案应该可以解决你的问题)
The error is telling you it can't find the library so no, that position is not correct.
http://wiki.apache.org/tomcat/HowTo#I.27m_encountering_classloader_problems_when_using_JNI_under_Tomcat
(The error in the FAQ is different, but the problem is the same and the solution there should solve your problem)
java 类不必位于 $CATALINA_HOME/shared/lib 中,而只需位于 .so 库中。因为即使执行以下操作也会出现相同的问题:
在catalina.properties中设置shared.loader=$CATALINA_HOME/shared/lib。
export LD_LIBRARY_PATH='/usr/local/tomcat/shared/lib/'
为什么还是找不到?我做错了什么所以tomcat看不到这个库?
感谢任何能提供帮助的人。
The java class doesn't have to be in the $CATALINA_HOME/shared/lib but only the .so library. Because is giving the same problem even with doing the following:
setting the shared.loader=$CATALINA_HOME/shared/lib in catalina.properties.
export LD_LIBRARY_PATH='/usr/local/tomcat/shared/lib/'
why is still not finding it? what am i doing wrong so tomcat can't see the library?
Thanks for whoever can help.
构建独立程序在 tomcat 上不起作用,因为 Web 应用程序结构中包含的独立类具有包名称,因此 tomcat 无法找到正确的路径,因为生成的本机库是来自独立应用程序的库没有包名。
Building a stand alone program didn't work on tomcat because the standalone class included in the web-app structure had a package name, so tomcat couldn't find the right path since the native library generated was the one from the stand alone app with no package name.