[英]No suitable native library found. native.libpath.* vs java.library.path
[英]Java native file not found, when native file is clearly in java.library.path
我正在處理一個 LWJGL 項目,我無法讓本機庫工作。 我可以在 IDE 中很好地運行它,但是當我導出它時,它崩潰了。 但問題是,我編寫了一些代碼來從 Jar 中提取本機文件,並將其放入一個臨時文件夾,完成后,它嘗試加載本機,但出現此錯誤:
java.lang.UnsatisfiedLinkError: no jinput-dx8 in java.library.path
at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1867)
at java.lang.Runtime.loadLibrary0(Runtime.java:870)
at java.lang.System.loadLibrary(System.java:1122)
at com.test.opengl.engineTester.NativeSupport.loadDLL(NativeSupport.java:46)
at com.test.opengl.engineTester.NativeSupport.extract(NativeSupport.java:23)
at com.test.opengl.engineTester.MainGameLoop.<clinit>(MainGameLoop.java:21)
NativeSupport.class:
import java.io.*;
import java.util.Date;
public class NativeSupport {
private static File tempLocation;
public static void extract() {
tempLocation = new File(System.getProperty("java.io.tmpdir") + "/OpenGL_Test_" + new Date().getTime() + "/");
tempLocation.deleteOnExit();
tempLocation.mkdirs();
System.out.println(System.getProperty("java.library.path"));
String path = tempLocation.getAbsolutePath()+"\\;";
System.setProperty("java.library.path", path);
System.out.println(path);
loadDLL("jinput-dx8.dll");
loadDLL("jinput-dx8_64.dll");
loadDLL("jinput-raw.dll");
loadDLL("jinput-raw_64.dll");
loadDLL("lwjgl.dll");
loadDLL("lwjgl64.dll");
loadDLL("OpenAL32.dll");
loadDLL("OpenAL64.dll");
}
private static void loadDLL(String name) {
try {
System.out.println(name);
BufferedReader in = new BufferedReader(new InputStreamReader(Class.class.getResourceAsStream("/"+name)));
File fileOut = new File(tempLocation, name);
System.out.println(fileOut.getAbsolutePath());
FileWriter out = new FileWriter(fileOut);
char[] buffer = new char[1024];
int length;
while ((length = in.read(buffer)) > 0) {
out.write(buffer, 0, length);
}
in.close();
out.close();
System.loadLibrary(fileOut.getName().substring(0, fileOut.getName().length()-4)); // Here is where the crash happens
} catch (Exception e) {
e.printStackTrace();
}
}
public static void clean() {
}
}
提取方法在 MainGameLoop.class 的靜態調用中調用。 我試過在主函數中調用它,它做了同樣的事情。 我還確保文件在加載之前存在。
我想避免 Jar Splice 程序。
如果我有什么不清楚的,我會在大約一天后回到這個問題來整理我的問題(我在 03:00 起床試圖解決這個問題)
謝謝你的幫助,我真的很感激。
前段時間我開發了示例代碼,可以在這里為您提供幫助。 看看這里:
https://github.com/mkowsiak/jnicookbook/tree/master/recipes/recipeNo031
您可以找到幾個組件:
在你的情況下,我會使用System.load而不是System.loadLibrary 。 無論如何,您的文件系統上都有該文件。
與 JNI 一起玩得開心。
有關 JNI 的更多示例,請查看此處: http : //jnicookbook.owsiak.org
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.