JNI Unsatisfied Link Error
I am getting the following error. Please advide where I may be going wrong.
Exception in thread "main" java.lang.UnsatisfiedLinkError: Plcio.open(Ljava/lang/String;)I at Plcio.open(Native Method) at Plcio.main(Plcio.java:11)
I am certain that the library is present in the path specified.
Plcio.java
public class Plcio {
private native int open(String plcName);
static {
//System.loadLibrary("test");
System.load("/home/usr/plcioExampleslib/libtest.so");
}
public static void main(String[] args) {
Plcio plcio = new Plcio();
int result = plcio.open("virtual");
System.out.println("result = " + result);
}
}
Plc.h
#ifndef _PLC_H
#define _PLC_H
#include<iostream>
#include<string>
#include<vector>
#include<plc.h>
#include<jni.h>
typedef PLC* plcPointer;
clas开发者_运维技巧s Plc{
public:
Plc() { }
Plc(const std::string &plctype, const std::vector<int> &data):_plctype(plctype),_data(data) {}
JNIEXPORT jint JNICALL Java_Plcio_open (JNIEnv *env, jobject jobj, jstring name) ;
private:
plcPointer _ptr;
const std::string _plctype;
std::vector<int> _data;
};
#endif
Plc.cpp
#include "Plc.h"
#include <jni.h>
using namespace std;
JNIEXPORT jint JNICALL Plc::Java_Plcio_open (JNIEnv *env, jobject jobj, jstring name) {
const char *plcname = (env)->GetStringUTFChars(name, 0);
_ptr = plc_open(const_cast<char*>(plcname));
env->ReleaseStringUTFChars(name, plcname);
if(_ptr == NULL) {
plc_print_error(_ptr, "plc_open\n");
return -1;
} else
cout << " open successfully " << endl;
return 0;
}
Regards,
-H
As far as I know, you cannot use C++ instance functions as JNI functions. The runtime does not have a reference to the instance of the C++ class.
You could try declaring the Plc::Java_Plcio_open
as static
, that should work (but of course, brings some implications).
Sorry but you are doing it very wrong. Please look up a tutoorial or some examples on JNI.
You are supposed to run javah on the class with the native method. This generates a C/C++ header file with a function declaration. It's signature of the form Java_package_name_classname_funcname. Generate the header file and then make sure your function signature is exactly the same.
First of all you probably haven't implemented all headers functions.
Do the following
1 - Browse to the .so directory 2 - Do:
$ ld libteste.so
And check if there are any non implemented functions :)
精彩评论