2017-07-27 35 views
0

我正在使用LWJGL項目,但無法使本機庫正常工作。我可以在IDE中運行它,但是當我導出它時,它會崩潰。但這裏的問題是,我做了一些代碼來提取的Jar本機文件,併到臨時文件夾,一旦做到這一點,它會嘗試加載本地,但它給這個錯誤:未找到Java本地文件,當本地文件顯然位於java.library.path中時

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試圖解決這個問題我了),對此,我真的非常感激。

+1

'java.library.path'應包含** **目錄包含DLL,DLL本身的不是名稱。在'extract()'方法中嘗試'tempLocation.getParent()。getAbsolutePath()'。 – Jesper

+0

@Jesper'tempLocation'是DLL的父目錄,當在loadDLL(name)方法中加載DLL時,tempLocation是父目錄編輯:嘗試過了,它仍然不起作用 – SevenDeLeven

回答

0

前一段時間我已經開發了可以幫助你的示例代碼。看看這裏:

https://github.com/mkowsiak/jnicookbook/tree/master/recipeNo031

你可以發現有幾個組件:

  • 庫提取(它需要照顧正從jar文件本機代碼)
  • HelloWorld類使用任意位置本機文件
  • 照顧所有東西的主類

在你的情況下,我會使用System.load而不是System.loadLibrary。無論如何,你的文件系統上都有這個文件。

與JNI玩得開心。

對於有關JNI更多的樣本,看看這裏:http://jnicookbook.owsiak.org

相關問題