views:

73

answers:

2

Hello,

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;

class 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

+2  A: 

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).

Dominik Fretz
A: 

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 :)

Marcos Roriz