我有一个EventReceiver.java文件(在com.applepark.jni包中),它调用编译到名为“libevent.so”的库中的本机方法startCommEngine(String,int,String)。程序启动时会出现以下错误:
Exception in thread "main" java.lang.UnsatisfiedLinkError: com.applepark.jni.EventReceiver.startCommEngine(Ljava/lang/String;ILjava/lang/String;)V
at com.applepark.jni.EventReceiver.startCommEngine(Native Method)
at com.applepark.jni.EventReceiver.start(EventReceiver.java:33)
at com.applepark.jni.HelloJNI.main(HelloJNI.java:12)
以下是我的EventReceiver.java文件:
package com.applepark.jni;
public class EventReceiver {
static {
String property = System.getProperty("user.dir");
System.load(property+"/lib/libevent.so");
System.out.println("Library 'libevent.so' load successful");
}
private String topic = "";
private String host = "";
private int port = 0;
public EventReceiver() {
this.topic = "/event/#";
this.host = "192.168.7.116";
this.port = 1883;
}
private native void startCommEngine(String ip, int port, String topic);
public void start(String ip,int port, String topic) {
startCommEngine(this.host,this.port,this.topic);
}
private native void stopCommEngine();
public void stop(){
stopCommEngine();
}
private native void publishData(String topic, byte[] data);
public void publish(String topic, byte[] data){
publishData(this.topic,data);
}
}
这是编译中使用的makefile:
CLASS_PATH := ../bin/com/applepark/jni
IPATH = /System/Library/Frameworks/JavaVM.framework/Headers
CUST_IPATH = ../src/com/applepark/jni
vpath %.class $(CLASS_PATH)
all : libevent.so
libevent.so : event_receiver.o
gcc -m64 -Wl, -shared -o $@ $< -lmosquitto
event_receiver.o : nldcs_mqtt_comm.c event_receiver.c com_applepark_jni_EventReceiver.c
gcc -I$(IPATH) -I$(CUST_IPATH) -m64 -c $< -o $@
com_applepark_jni_EventReceiver.h : EventReceiver.class
javah -jni -classpath $(CLASS_PATH) $*
clean :
rm event_receiver.o libevent.so
这是我的Native函数实现:
#include <stdlib.h>
#include <stdio.h>
#include "com_applepark_jni_EventReceiver.h"
#include "event_receiver.h"
#include "nldcs_mqtt_comm.h"
JNIEXPORT void JNICALL Java_com_applepark_jni_EventReceiver_startCommEngine
(JNIEnv *env, jobject ctx, jstring host_str, jint port, jstring topic) {
printf("startCommEngine : Initialising the Receiver \n");
}
无法理解发生了什么。