Я сделал это, используя stati c Класс Loader следующим образом:
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");
}
}
}
Там я загружаю библиотечный файл из ресурсов и копирую его содержимое во временный каталог. Как вы можете видеть перед этим, я установил jna.library.path во временную папку, поэтому JNA будет искать там библиотеки.
Далее я загружаю библиотеку так:
MyLibClass instance = (MyLibClass) Native.loadLibrary(Loader.getNative(), MyLibClass.class);