简体   繁体   中英

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. I can run it in the IDE just fine, but when I export it, it crashes. 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:

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() {
}

}

The extract method is called in a static call from 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.

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)

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

You can find there few components:

  • library extractor (it takes care for getting native code from jar file)
  • HelloWorld class that uses arbitrary located native file
  • Main class that takes care of everything

In your case, I'd use System.load instead of System.loadLibrary . You have the file anyway on your file system.

Have fun with JNI.

For more samples related to JNI, take a look here: http://jnicookbook.owsiak.org

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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