tomcat下的java.lang.UnsatisfiedLinkError

时间:2010-07-01 06:43:16

标签: java tomcat linker java-native-interface

我有一个java类(eval.java),它调用so文件(libmodel.so)中的本机方法。我可以使用System.loadLibrary()来加载libmodel.so而不会出现任何错误(-Djava.library.path = / usr / lib),并且本机方法initModel()可以正常工作。

但是,当我将其部署为Web服务(tomcat 6.0.26 + axis2 1.5.1 + eclipse Java EE helios)来调用本机方法时,我收到以下错误(第一行表示.so成功加载):

Load library successfully
[ERROR] com.model.modelJNI.initModel()V
java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.apache.axis2.rpc.receivers.RPCUtil.invokeServiceClass(RPCUtil.java:194)
    at org.apache.axis2.rpc.receivers.RPCMessageReceiver.invokeBusinessLogic(RPCMessageReceiver.java:102)
    at org.apache.axis2.receivers.AbstractInOutMessageReceiver.invokeBusinessLogic(AbstractInOutMessageReceiver.java:40)
    at org.apache.axis2.receivers.AbstractMessageReceiver.receive(AbstractMessageReceiver.java:114)
    at org.apache.axis2.engine.AxisEngine.receive(AxisEngine.java:173)
    at org.apache.axis2.transport.http.HTTPTransportUtils.processHTTPPostRequest(HTTPTransportUtils.java:167)
    at org.apache.axis2.transport.http.AxisServlet.doPost(AxisServlet.java:142)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:852) 
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
    at java.lang.Thread.run(Thread.java:619)
Caused by: java.lang.UnsatisfiedLinkError: com.model.modelJNI.initModel()V
    at com.model.modelJNI.initModel(Native Method)
    at com.model.model.initModel(model.java:13)
    at com.webservice.run(EvalVita.java:763)
    ... 25 more

以下是Eval.java的正文

import com.model.*;

public class Eval {
  static void loadModel() {
    try {
        System.loadLibrary("model");
        System.out.println("Load library successfully");
    } catch (UnsatisfiedLinkError e) {
        System.err.println("Native code library failed to load." + e);
    }
  }
  public void run() {
    loadModel();
    model.initModel();
  }
}

我在这里遗漏了什么?任何意见都表示赞赏!

1 个答案:

答案 0 :(得分:2)

这可能意味着已成功加载,但无法找到JNI所需的单个函数(或未正确声明)。以下是C代码的示例:

/*
 * Class:     com_model_modelJNI
 * Method:    initModel
 * Signature: ()V
 */
JNIEXPORT void JNICALL Java_com_model_modelJNI_initModel
  (JNIEnv * env, jclass jclazz)
{
  // etc.
}

我之前从未使用过Swig,所以我不确定它是如何与JNI一起使用的。但您可能想尝试使用javah并查看输出内容,进行比较。