Reputation: 23
I am actually trying to open a library. So (I did it in c++) with JNA my problem is that I can not open a library when there is a static variable. However I have to use un singleton in my library so I am looking for why a static variable can not be use with JNA to prove it a made a little library
there is the .h file:
#ifndef UNTITLED1_LIBRARY_H
#define UNTITLED1_LIBRARY_H
#include <iostream>
class library {
private:
static char* h;
public:
int hello();
};
extern "C" int hello(){
library lib;
return lib.hello();
}
#endif
then my .cpp file:
#include "library.h"
#include "ecrire.h"
#include <iostream>
int library::hello() {
h = (char*)"hello world";
std::cout<<h<<std::endl;
return 45;
}
then my java class and interface
public class Westgard {
static {
System.setProperty("jna.library.path","../logic/resources/calculator");
}
public static void main(String[] args) {
// TODO Auto-generated method stub
int maj = InterfaceLibWestgard.INSTANCE.hello();
System.out.println(maj);
}
}
import com.sun.jna.Library;
import com.sun.jna.Native;
public interface InterfaceLibWestgard extends Library {
int hello();
static InterfaceLibWestgard INSTANCE = (InterfaceLibWestgard)
Native.loadLibrary("../logic/resources/calculator/libuntitled1.so",
InterfaceLibWestgard.class);
}
So if I try like this it wont work but when a remove the static from the .h it works does not anyone know why I have been looking for since 4-5 hours still do not know why...
This is my issue log:
Exception in thread "main" java.lang.UnsatisfiedLinkError: Unable to load
library '../logic/resources/calculator/libuntitled1.so': Native library
(linux-x86-64/../logic/resources/calculator/libuntitled1.so) not found in
resource path ([file:/opt/java/jdk1.8.0_151/jre/lib/resources.jar,
Upvotes: 2
Views: 2584
Reputation: 67
You could use Native.load()
instead of Native.loadLibrary()
.
Check the code below,
public interface MyLibrary extends Library {
int myMethod();
}
static {
MyLibrary lib = (MyLibrary)Native.load("untitled1" , MyLibrary.class);
}
You can add the file in the resource location and load it in the above manner easily. You should exclude the "lib" prefix and ".so" file type. In your case,
filename = "untitled1"
Using JNA to Access Native Dynamic Libraries
Upvotes: 1
Reputation: 36603
You have declared library::h
but not defined it. You need to add
char* library::h = 0;
in your cpp file.
Presumably the library is either failing to compile or it is compiling but expecting this missing symbol to be defined in another library.
Upvotes: 1