简体   繁体   English

未找到 Java 本机文件,当本机文件明确位于 java.library.path 中时

[英]Java native file not found, when native file is clearly in java.library.path

I am working on a LWJGL project, and I cannot get the native libraries to work.我正在处理一个 LWJGL 项目,我无法让本机库工作。 I can run it in the IDE just fine, but when I export it, it crashes.我可以在 IDE 中很好地运行它,但是当我导出它时,它崩溃了。 But here is the problem, I made a bit of code to extract the native files from the Jar, and into a temporary folder, once that is done, it attempts to load the native, but it gives this error:但问题是,我编写了一些代码来从 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: 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() {
}

}

The extract method is called in a static call from MainGameLoop.class.提取方法在 MainGameLoop.class 的静态调用中调用。 I have tried calling it in the main function, and it did the same thing.我试过在主函数中调用它,它做了同样的事情。 I also made sure that the files existed before they were loaded.我还确保文件在加载之前存在。

I would like to avoid the Jar Splice program.我想避免 Jar Splice 程序。

If I was unclear about anything, I will be back with this question in about a day to tidy up my question (I am up at 03:00 trying to solve this problem)如果我有什么不清楚的,我会在大约一天后回到这个问题来整理我的问题(我在 03:00 起床试图解决这个问题)

Thanks for your help, I really appreciate it.谢谢你的帮助,我真的很感激。

Some time ago I have developed sample code that can help you here.前段时间我开发了示例代码,可以在这里为您提供帮助。 Take a look here:看看这里:

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

You can find there few components:您可以找到几个组件:

  • library extractor (it takes care for getting native code from jar file)库提取器(它负责从 jar 文件中获取本机代码)
  • HelloWorld class that uses arbitrary located native file使用任意定位的本机文件的 HelloWorld 类
  • Main class that takes care of everything处理一切的主类

In your case, I'd use System.load instead of System.loadLibrary .在你的情况下,我会使用System.load而不是System.loadLibrary You have the file anyway on your file system.无论如何,您的文件系统上都有该文件。

Have fun with JNI.与 JNI 一起玩得开心。

For more samples related to JNI, take a look here: http://jnicookbook.owsiak.org有关 JNI 的更多示例,请查看此处: http : //jnicookbook.owsiak.org

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM