繁体   English   中英

未找到 Java 本机文件,当本机文件明确位于 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

您可以找到几个组件:

  • 库提取器(它负责从 jar 文件中获取本机代码)
  • 使用任意定位的本机文件的 HelloWorld 类
  • 处理一切的主类

在你的情况下,我会使用System.load而不是System.loadLibrary 无论如何,您的文件系统上都有该文件。

与 JNI 一起玩得开心。

有关 JNI 的更多示例,请查看此处: http : //jnicookbook.owsiak.org

暂无
暂无

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

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