mzereba
mzereba

Reputation: 2517

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.

Upvotes: 0

Views: 2717

Answers (3)

mzereba
mzereba

Reputation: 2517

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.

Upvotes: 0

mzereba
mzereba

Reputation: 2517

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.

Upvotes: 1

Brian Roach
Brian Roach

Reputation: 76888

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)

Upvotes: 2

Related Questions