jna.loadLibrary no puede encontrar el archivo de biblioteca nativa

Borune:

He creado propio frasco con envoltorio biblioteca nativa. La estructura de la resultante tarro es:

library.jar
 |- com (there are my .java classes)
 |- libs (there is the native - libmylib.so)
 |- META-INF

Me carga lib nativa de la siguiente manera:

MyLibClass instance = (MyLibClass) Native.loadLibrary("mylib", MyLibClass.class);

Ahora quiero añadir esta biblioteca en otro proyecto y lo uso. Sin embargo, cuando se crea una instancia de MyLibClass recibo un error:

Exception in thread "main" java.lang.UnsatisfiedLinkError: Unable to load library 'mylib':
libmylib.so: cannot open shared object file: No such file or directory

¿Cómo debo solucionar este problema?

Borune:

He hecho que el uso de clase Loader estática de la siguiente manera:

static class Loader {

    private Loader() {
    }

    static String getNative() {
        InputStream in = null;
        FileOutputStream fos = null;
        File fileOut = null;
        System.setProperty("jna.library.path",
                System.getProperty("java.io.tmpdir"));

        in = Loader.class.getResourceAsStream(
                        "/libs/libmylib.so");

        if (in != null) {
            try {
                fileOut = File.createTempFile("mylib", ".so");
                fileOut.deleteOnExit();

                fos = new FileOutputStream(fileOut);

                int count;
                byte[] buf = new byte[1024];

                while ((count = in.read(buf, 0, buf.length)) > 0) {
                    fos.write(buf, 0, count);
                }

            } catch (IOException ex) {
                throw new Error("Failed to create temporary file: " + ex);
            } finally {
                try {
                    in.close();
                } catch (IOException ex) {
                }

                if (fos != null) {
                    try {
                        fos.close();
                    } catch (IOException ex) {
                    }
                }

                return fileOut.getAbsolutePath();
            }
        } else {
            throw new Error("Couldn't open native library file");
        }
    }
}

Hay que cargar el archivo de la biblioteca de recursos y copiar sus contenidos en el directorio temporal. Como se puede ver antes de hacer eso me puse a jna.library.path carpeta de la temperatura, por lo JNA buscará bibliotecas allí.

Futher Estoy cargando biblioteca como esta:

MyLibClass instance = (MyLibClass) Native.loadLibrary(Loader.getNative(), MyLibClass.class);

Supongo que te gusta

Origin http://43.154.161.224:23101/article/api/json?id=365764&siteId=1
Recomendado
Clasificación