简体   繁体   中英

Error calling native library in Java from .so in tomcat

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.

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)

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:

  1. setting the shared.loader=$CATALINA_HOME/shared/lib in catalina.properties.

  2. 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-app结构中包含的独立类具有程序包名称,因此tomcat找不到正确的路径,因为生成的本机库是来自独立应用程序并带有没有软件包名称。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM